swapping bytes in an integer

Discussion in 'C Programming' started by john@fcs.uga.edu, May 8, 2006.

  1. Guest

    If I have a 32 bit unsigned int that is in the wrong byte order, how
    can I convert it? For example, if have a number 0x090a0b0c how can I
    reverse this to 0x0c0b0a09 ?

    Thanks,
    -John
     
    , May 8, 2006
    #1
    1. Advertising

  2. W Marsh Guest

    On 8 May 2006 14:03:08 -0700, wrote:

    >If I have a 32 bit unsigned int that is in the wrong byte order, how
    >can I convert it? For example, if have a number 0x090a0b0c how can I
    >reverse this to 0x0c0b0a09 ?
    >
    >Thanks,
    >-John


    The shift operators << and >> shift a value left and right by one bit,
    respectively. Shifting by 4 bits will move the value by one hex digit.
    You can combine values with the bitwise OR operator, |
     
    W Marsh, May 8, 2006
    #2
    1. Advertising

  3. Flash Gordon Guest

    wrote:
    > If I have a 32 bit unsigned int that is in the wrong byte order, how
    > can I convert it? For example, if have a number 0x090a0b0c how can I
    > reverse this to 0x0c0b0a09 ?


    Depends on whether you want to convert it in place or not. Anyway, the
    simple way to understand is using shift operators >> and << and bitwise
    and & for masking.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 8, 2006
    #3
  4. Ian Collins Guest

    wrote:
    > If I have a 32 bit unsigned int that is in the wrong byte order, how
    > can I convert it? For example, if have a number 0x090a0b0c how can I
    > reverse this to 0x0c0b0a09 ?
    >

    It my be non-standard, bit if you system has the networking library
    function ntohl and you don't require portability, you can use that.

    --
    Ian Collins.
     
    Ian Collins, May 8, 2006
    #4
  5. Tomás Guest


    > Monkey monkey = 0x090a0b0c;



    Opps! Should have written:


    Monkey monkey;

    monkey.four_bytes = 0x090a0b0c;


    -Tomás
     
    Tomás, May 8, 2006
    #5
  6. W Marsh Guest

    On Mon, 08 May 2006 21:35:03 GMT, "Tomás" <> wrote:

    >
    >> Monkey monkey = 0x090a0b0c;

    >
    >
    >Opps! Should have written:
    >
    >
    > Monkey monkey;
    >
    > monkey.four_bytes = 0x090a0b0c;
    >
    >
    >-Tomás


    It makes no difference - we have no idea what Monkey is anyway. It was
    probably simpler the first way, we could just assume it was an integer
    of some description.
     
    W Marsh, May 8, 2006
    #6
  7. W Marsh <> writes:
    > On 8 May 2006 14:03:08 -0700, wrote:
    >>If I have a 32 bit unsigned int that is in the wrong byte order, how
    >>can I convert it? For example, if have a number 0x090a0b0c how can I
    >>reverse this to 0x0c0b0a09 ?

    >
    > The shift operators << and >> shift a value left and right by one bit,
    > respectively. Shifting by 4 bits will move the value by one hex digit.
    > You can combine values with the bitwise OR operator, |


    No, the shift operators shift a value by a specified number of bits.
    For example, x<<1 yields x shifted left by one bit, x<<4 yields x
    shifted left by 4 bits, and x<<N yields x shifted left by N bits.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 8, 2006
    #7
  8. Ian Collins wrote:
    > wrote:
    > > If I have a 32 bit unsigned int that is in the wrong byte order, how
    > > can I convert it? For example, if have a number 0x090a0b0c how can I
    > > reverse this to 0x0c0b0a09 ?
    > >

    > It my be non-standard, bit if you system has the networking library
    > function ntohl and you don't require portability, you can use that.


    If I understand correctly, ntohl() converts a big endian integer to a
    system integer, meaning if system integers are already big endian, it
    simply returns whatever you pass it. So it may not be useful even when
    it does exist.

    Treating the number when it has the wrong byte order as an array of
    unsigned char, and manually combining octets (or manually splitting the
    number into them, if you have to go the other way), is a more portable
    alternative, which if done right should work on any system.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, May 8, 2006
    #8
  9. W Marsh Guest

    On Mon, 08 May 2006 21:53:59 GMT, Keith Thompson <>
    wrote:

    >W Marsh <> writes:
    >> On 8 May 2006 14:03:08 -0700, wrote:
    >>>If I have a 32 bit unsigned int that is in the wrong byte order, how
    >>>can I convert it? For example, if have a number 0x090a0b0c how can I
    >>>reverse this to 0x0c0b0a09 ?

    >>
    >> The shift operators << and >> shift a value left and right by one bit,
    >> respectively. Shifting by 4 bits will move the value by one hex digit.
    >> You can combine values with the bitwise OR operator, |

    >
    >No, the shift operators shift a value by a specified number of bits.
    >For example, x<<1 yields x shifted left by one bit, x<<4 yields x
    >shifted left by 4 bits, and x<<N yields x shifted left by N bits.


    I meant to write "shift a value left and right through bits", yes.
     
    W Marsh, May 8, 2006
    #9
  10. Ian Collins Guest

    Harald van Dijk wrote:
    > Ian Collins wrote:
    >
    >> wrote:
    >>
    >>>If I have a 32 bit unsigned int that is in the wrong byte order, how
    >>>can I convert it? For example, if have a number 0x090a0b0c how can I
    >>>reverse this to 0x0c0b0a09 ?
    >>>

    >>
    >>It my be non-standard, bit if you system has the networking library
    >>function ntohl and you don't require portability, you can use that.

    >
    >
    > If I understand correctly, ntohl() converts a big endian integer to a
    > system integer, meaning if system integers are already big endian, it
    > simply returns whatever you pass it. So it may not be useful even when
    > it does exist.
    >

    Yes, you are correct. My advice was wrong.

    --
    Ian Collins.
     
    Ian Collins, May 8, 2006
    #10
  11. Guest

    Ian Collins wrote:
    > wrote:
    > > If I have a 32 bit unsigned int that is in the wrong byte order, how
    > > can I convert it? For example, if have a number 0x090a0b0c how can I
    > > reverse this to 0x0c0b0a09 ?
    > >

    > It my be non-standard, bit if you system has the networking library
    > function ntohl and you don't require portability, you can use that.
    >
    > --
    > Ian Collins.


    I just tried ntohl() in my program and this works perfectly. I think I
    will use this method. I want to thank everyone who replied for all of
    their great ideas! This help is greatly appreciated.

    -John
     
    , May 8, 2006
    #11
  12. W Marsh Guest

    On 8 May 2006 15:18:42 -0700, wrote:

    >Ian Collins wrote:
    >> wrote:
    >> > If I have a 32 bit unsigned int that is in the wrong byte order, how
    >> > can I convert it? For example, if have a number 0x090a0b0c how can I
    >> > reverse this to 0x0c0b0a09 ?
    >> >

    >> It my be non-standard, bit if you system has the networking library
    >> function ntohl and you don't require portability, you can use that.
    >>
    >> --
    >> Ian Collins.

    >
    >I just tried ntohl() in my program and this works perfectly. I think I
    >will use this method. I want to thank everyone who replied for all of
    >their great ideas! This help is greatly appreciated.
    >
    >-John


    Then bear in mind that it's not portable, and will have no effect when
    compiled for a machine with a different endianess to your own.
     
    W Marsh, May 8, 2006
    #12
  13. On 2006-05-08, W Marsh <> wrote:
    > On 8 May 2006 15:18:42 -0700, wrote:
    >
    >>Ian Collins wrote:
    >>> wrote:
    >>> > If I have a 32 bit unsigned int that is in the wrong byte order, how
    >>> > can I convert it? For example, if have a number 0x090a0b0c how can I
    >>> > reverse this to 0x0c0b0a09 ?
    >>> >
    >>> It my be non-standard, bit if you system has the networking library
    >>> function ntohl and you don't require portability, you can use that.
    >>>
    >>> --
    >>> Ian Collins.

    >>
    >>I just tried ntohl() in my program and this works perfectly. I think I
    >>will use this method. I want to thank everyone who replied for all of
    >>their great ideas! This help is greatly appreciated.
    >>
    >>-John

    >
    > Then bear in mind that it's not portable, and will have no effect when
    > compiled for a machine with a different endianess to your own.


    If his code depends on big-endianness, then it doesn't matter that the function
    has no effect; it simply means that the machine is already set up correctly
    for your functions.

    I worded that really poorly, but basically I mean that it is irrelevant what
    effect a certain function actually has; only the end result is important.
     
    Andrew Poelstra, May 8, 2006
    #13
  14. W Marsh Guest

    On Mon, 08 May 2006 22:41:34 GMT, Andrew Poelstra
    <> wrote:

    >On 2006-05-08, W Marsh <> wrote:
    >> On 8 May 2006 15:18:42 -0700, wrote:
    >>
    >>>Ian Collins wrote:
    >>>> wrote:
    >>>> > If I have a 32 bit unsigned int that is in the wrong byte order, how
    >>>> > can I convert it? For example, if have a number 0x090a0b0c how can I
    >>>> > reverse this to 0x0c0b0a09 ?
    >>>> >
    >>>> It my be non-standard, bit if you system has the networking library
    >>>> function ntohl and you don't require portability, you can use that.
    >>>>
    >>>> --
    >>>> Ian Collins.
    >>>
    >>>I just tried ntohl() in my program and this works perfectly. I think I
    >>>will use this method. I want to thank everyone who replied for all of
    >>>their great ideas! This help is greatly appreciated.
    >>>
    >>>-John

    >>
    >> Then bear in mind that it's not portable, and will have no effect when
    >> compiled for a machine with a different endianess to your own.

    >
    >If his code depends on big-endianness, then it doesn't matter that the function
    >has no effect; it simply means that the machine is already set up correctly
    >for your functions.
    >
    >I worded that really poorly, but basically I mean that it is irrelevant what
    >effect a certain function actually has; only the end result is important.


    No - he asked how to swap bytes, not how to manifest a specific
    endianess.

    If he has data he wants to treat natively, then ntohl is fine.

    If he actually wants to swap the bytes of an integer in any case
    (implementing a swap opcode in a CPU emulator, for example), he won't
    be able to use ntohl.
     
    W Marsh, May 8, 2006
    #14
  15. EventHelix.com, May 9, 2006
    #15
  16. Guest

    EventHelix.com wrote:
    > > If I have a 32 bit unsigned int that is in the wrong byte order, how
    > > can I convert it? For example, if have a number 0x090a0b0c how can I
    > > reverse this to 0x0c0b0a09 ?

    >
    > The following article should help:
    > http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm
    >
    > --
    > EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
    > Sequence Diagram Based System Design and Object Interaction Modeling
    > Tool


    >From this web site...


    Routines to convert between big-endian and little-endian formats are
    actually quite straight forward. The routines shown below will convert
    from both ways, i.e. big-endian to little-endian and back.
    Big-endian to Little-endian conversion and back


    short convert_short(short in)
    {
    short out;
    char *p_in = (char *) &in;
    char *p_out = (char *) &out;
    p_out[0] = p_in[1];
    p_out[1] = p_in[0];
    return out;
    }

    long convert_long(long in)
    {
    long out;
    char *p_in = (char *) &in;
    char *p_out = (char *) &out;
    p_out[0] = p_in[3];
    p_out[1] = p_in[2];
    p_out[2] = p_in[1];
    p_out[3] = p_in[0];
    return out;
    }
     
    , May 9, 2006
    #16
  17. <> wrote in message
    news:...
    > If I have a 32 bit unsigned int that is in the wrong byte order, how
    > can I convert it? For example, if have a number 0x090a0b0c how can I
    > reverse this to 0x0c0b0a09 ?
    >


    It seems you've found ntohl(), but this is how:

    unsigned long a,b;

    a=0x090a0b0c;
    b=((a&0xFF000000)>>24)|((a&0x00FF0000)>>8)|((a&0x0000FF00)<<8)|((a&0x000000F
    F)<<24);

    Of course, you can drop any place holder zeros between 0x and FF.


    Rod Pemberton
     
    Rod Pemberton, May 9, 2006
    #17
  18. Eric Sosman Guest

    wrote On 05/08/06 18:18,:
    > Ian Collins wrote:
    >
    >> wrote:
    >>
    >>>If I have a 32 bit unsigned int that is in the wrong byte order, how
    >>>can I convert it? For example, if have a number 0x090a0b0c how can I
    >>>reverse this to 0x0c0b0a09 ?
    >>>

    >>
    >>It my be non-standard, bit if you system has the networking library
    >>function ntohl and you don't require portability, you can use that.
    >>
    >>--
    >>Ian Collins.

    >
    >
    > I just tried ntohl() in my program and this works perfectly. I think I
    > will use this method. I want to thank everyone who replied for all of
    > their great ideas! This help is greatly appreciated.


    <off-topic reason="potential bug">

    Be careful: ntohl() does *not* do what you asked.
    It converts "network byte order" (Big-Endian) to "host
    byte order" (whatever your machine uses). If your
    machine uses Big-Endian byte order already, ntohl()
    will not swap the bytes: it will do nothing at all.
    To put it another way, ntohl() may in fact do what you
    want on the machine you're using at the moment, but
    will definitely *not* do what you want on all machines.

    If you need to swap the bytes unconditionally, no
    matter what machine you're using, you'll have to work
    a little harder.

    </off-topic>

    --
     
    Eric Sosman, May 9, 2006
    #18
  19. Guest

    Wow! This code is really awesome. I think this is the absolute best
    way I have seen yet. I will have to study your code and break it down
    to see how it works exaclty. Do you think that it could it be placed
    within a #define ?

    Thanks,
    -John
     
    , May 9, 2006
    #19
  20. Eric Sosman <> writes:
    > wrote On 05/08/06 18:18,:
    >> Ian Collins wrote:
    >>> wrote:
    >>>>If I have a 32 bit unsigned int that is in the wrong byte order, how
    >>>>can I convert it? For example, if have a number 0x090a0b0c how can I
    >>>>reverse this to 0x0c0b0a09 ?
    >>>
    >>>It my be non-standard, bit if you system has the networking library
    >>>function ntohl and you don't require portability, you can use that.

    >>
    >> I just tried ntohl() in my program and this works perfectly. I think I
    >> will use this method. I want to thank everyone who replied for all of
    >> their great ideas! This help is greatly appreciated.

    >
    > <off-topic reason="potential bug">
    >
    > Be careful: ntohl() does *not* do what you asked.
    > It converts "network byte order" (Big-Endian) to "host
    > byte order" (whatever your machine uses). If your
    > machine uses Big-Endian byte order already, ntohl()
    > will not swap the bytes: it will do nothing at all.
    > To put it another way, ntohl() may in fact do what you
    > want on the machine you're using at the moment, but
    > will definitely *not* do what you want on all machines.
    >
    > If you need to swap the bytes unconditionally, no
    > matter what machine you're using, you'll have to work
    > a little harder.


    We're not certain exactly what the OP's requirements are, only what he
    stated them to be. He might need a general solution to swapping bytes
    (in which case ntohl() won't do it), or it may be that the only time
    he'll see a number in the wrong byte order is when it comes from a
    network (in which case ntohl() is probably just the thing).

    > </off-topic>


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 9, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Collins
    Replies:
    3
    Views:
    6,101
    Jason Collins
    Feb 18, 2004
  2. mrby

    4-bytes or 8-bytes alignment?

    mrby, Nov 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    454
    Mark McIntyre
    Nov 2, 2004
  3. Replies:
    5
    Views:
    584
    Flash Gordon
    Apr 9, 2006
  4. Yandos
    Replies:
    12
    Views:
    5,160
    Pete Becker
    Sep 15, 2005
  5. Replies:
    8
    Views:
    531
    Bob Hairgrove
    Apr 10, 2006
Loading...

Share This Page