Thread: HEX swapnibbles

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    HEX swapnibbles

    So I got my code to prompt the user for a hex number and output a hex number. I'm trying to swap the numbers for them now. ie 0x12 would be displayed 0x21 etc. When I try to >> the number it does the whole number and not the individual characters. How do I get it to swap the individual chars? Any help would be great.

    Code:
    #include <stdio.h>
    
    unsigned char data (void)
    {
        unsigned char data;
    
        printf ("\nEnter a hexadecimal value from 0x00 to 0xFF:\n");
        scanf ("%hhx", &data);
        printf ("In reverse, that is 0x%02\n", data);
    
        return data;
    }
    
    char main (void)
    {
          data ();
    
          return 0;
    }
    Last edited by et3ruiz; 11-23-2011 at 04:34 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That is nowhere near valid C code. main is supposed to return an int, not a char. The first set of code in the curly braces wont compile since there's no function name. You should name your function something more sensible than "data", like "swap_nibble". It should take data as a parameter, and return the swapped version. Also, you need a % in your scanf call: "%hhx", and you need to pass the address of data, i.e. &data.

    You really need to review your C basics. We have some tutorials (C, C++ Programming Tutorials - Cprogramming.com), and Google will give you lots more. Work lots of examples. Perhaps read a few function tutorials (start here: Functions in C - Cprogramming.com). Also, read up on bitwise operators (again, here: Cprogramming.com - Tutorials - Bitwise Operators and Bit Manipulations in C and C++). >> shifts everything to the right. You need to shift the left half to the right, and the right half to the left, then combine them.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Sorry about that. This is what my code really looks like. I was trying to put it up quick to tend to my baby.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Ahh, much better. I still don't see you actually attempting to swap the halves. Unfortunately we don't really hand out code here (basically a homework policy thing), so some attempt at making it work is needed on your part. What did you try with >> that didn't work?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    When I put data >> 8, (or whatever value) on the printf line, I'm just adjusting the whole value. Not the individual digits.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, first off, you should only use shift operations on unsigned data (like you are now), since the behavior on signed data can be undefined or implementation defined.

    >> moves everything to the right a specified amount of bits/spaces. Note that a byte, signed or unsigned, is 8 bits (with very, very rare exception), so byte >> 8 will always be 0. You shift all the bits off the right end. It doesn't wrap around.

    A nibble is half a byte, or 4 bits. You need to shift data to the right one nibble to move the left half to the right. You also need to shift data to the left one nibble to move the right half to the left. You need to combine those two results with a bitwise OR:
    Code:
    data = (data shifted left 1 nibble) | (data shifted right one nibble);

Popular pages Recent additions subscribe to a feed