changing byte order of an unsigned long?

This is a discussion on changing byte order of an unsigned long? within the C Programming forums, part of the General Programming Boards category; Hi all, i'm a newbie to this forum so be gentle! I have a problem i've spent quite a bit ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    changing byte order of an unsigned long?

    Hi all, i'm a newbie to this forum so be gentle!

    I have a problem i've spent quite a bit of time tinkering with and can't seem to get anywhere.

    I'm trying to write code that takes an unsigned long and reverses the order of the bytes.

    For example changing B1B2B3B4 to B4B3B2B1.

    Like i said i'm not getting anywhere with this so could anyone at least point me in the right direction?

    Thanks
    Stu

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Well to extract B2, you would do
    b2 = ( var >> 16 ) & 0xFF;

    To put it back in the new position
    result = result | ( b2 << 8 );

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    ...and if you don't understand a bit of that, then just google "Bitwise operations"

    ...and by the way, Salem, your new user title is genius.
    Sent from my iPadŽ

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    These may also be of interest:

    htonl, htons, ntohl, ntohs - convert values between host and network byte order
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Hi Salem,

    I've had a play with bitwise ops and i can do what you suggested to extract they bytes, but i can't put them back in the reverse order like i'm trying to do.

    I also have a problem in that if one of the bytes is 0xF or less, i have a zero which disappears. I.e 0F is displayed as F.

    So when i reorder the bytes the final number might not look right, see below.


    number F54C09A1 -> in reverse order would be A1094CF5

    but the zero disappears when i take 0900 out of F54C09A1.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Show your code.
    If you just print with %x, then leading zeros will be suppressed.
    Try %04x or %08x

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Thanks Salem,

    I've got it sorted now. I had to use %02x.

    Code:
    b1 = ( input_val >> 24 ) & 0xFF;	//Splitting the bytes
    b2 = ( input_val >> 16 ) & 0xFF;
    b3 = ( input_val >> 8 ) & 0xFF;
    b4 = ( input_val >> 0 ) & 0xFF;
    
    
    
    printf("\nCorrect order is: %02x%02x%02x%02x", b4,b3,b2,b1);	//rearranging the bytes in reverse order.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 01:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21