Swapping byte order, aka Big endian?

This is a discussion on Swapping byte order, aka Big endian? within the C Programming forums, part of the General Programming Boards category; Hello, hope someone can help me. I was wondering if there was an easy, portable way(I know I'm asking alot) ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Swapping byte order, aka Big endian?

    Hello, hope someone can help me. I was wondering if there was an easy, portable way(I know I'm asking alot) to switch the byte order of an integer. From what I've found so far, I can use some functions in winsock2.h, but I can't seem to link it, and it isn't very portable anyways.

    In case I'm being a little unclear, here's what i mean:

    I have an int that outputs to a file as (in hex) A0 13 00 00, but I need it to output as 00 00 13 A0.

    Thanks in advance.
    Last edited by Alexlf; 01-14-2011 at 03:33 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What's wrong with ntohl?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Which header would I have to include for that? Sorry if it's obvious.

    EDIT
    I suppose it would help to know that I'm on windows.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Oh, looks like I had forgotten to add -lws2_32 to my linker. I managed to get Winsock.h to work, and ntohl did the trick. Thanks very much for the help.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    26
    When in doubt, consult the documentation:

    ntohl Function (Windows)

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    Careful with ntohl(). First off, as you note, it's not portable (it exists on unix systems, but of course not in winsock2.h). Second, it won't do any swapping if the native system is big endian: it converts network byte-order (big endian) to host byte-order (which could also be big endian).

    If you want to swap a four-byte integer in all cases, just write your own swap function; it's easy enough to do. If you do only want to swap if the native system is little endian, ntohl() is probably the best method, but you'll have to deal with the whole portability issue.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cas View Post
    Careful with ntohl(). First off, as you note, it's not portable (it exists on unix systems, but of course not in winsock2.h).
    Yep... it's in winsock as well. You will probably need to add ws2_32.lib or ws2_64 to your linker's list.


    If you want to swap a four-byte integer in all cases, just write your own swap function; it's easy enough to do. If you do only want to swap if the native system is little endian, ntohl() is probably the best method, but you'll have to deal with the whole portability issue.
    I agree he should probably write his own, unless he's doing sockets programming....
    Last edited by CommonTater; 01-15-2011 at 08:56 AM.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Here is something that should be portable:

    Code:
            unsigned long int m, n;
    
            ...
    
            m =  (n & 0xff000000) >> 24;
            m |= (n & 0x00ff0000) >> 8;
            m |= (n & 0x0000ff00) << 8;
            m |= (n & 0x000000ff) << 24;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary File - Byte order / endian
    By chico1st in forum C Programming
    Replies: 27
    Last Post: 08-22-2007, 07:13 PM
  2. byte order change
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 08-06-2007, 05:40 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. changing byte order of an unsigned long?
    By stustu92 in forum C Programming
    Replies: 6
    Last Post: 01-27-2006, 05:21 AM
  5. byte and bit order question
    By chunlee in forum C Programming
    Replies: 7
    Last Post: 11-07-2004, 12:50 PM

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