Velocity Reviews > swapping bytes in an integer

# swapping bytes in an integer

john@fcs.uga.edu
Guest
Posts: n/a

 05-08-2006
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

W Marsh
Guest
Posts: n/a

 05-08-2006
On 8 May 2006 14:03:08 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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, |

Flash Gordon
Guest
Posts: n/a

 05-08-2006
(E-Mail Removed) 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
--
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

Ian Collins
Guest
Posts: n/a

 05-08-2006
(E-Mail Removed) 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.

Tomás
Guest
Posts: n/a

 05-08-2006

> Monkey monkey = 0x090a0b0c;

Opps! Should have written:

Monkey monkey;

monkey.four_bytes = 0x090a0b0c;

-Tomás

W Marsh
Guest
Posts: n/a

 05-08-2006
On Mon, 08 May 2006 21:35:03 GMT, "Tomás" <(E-Mail Removed)> 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.

Keith Thompson
Guest
Posts: n/a

 05-08-2006
W Marsh <(E-Mail Removed)> writes:
> On 8 May 2006 14:03:08 -0700, (E-Mail Removed) 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) (E-Mail Removed) <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.

=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a

 05-08-2006
Ian Collins wrote:
> (E-Mail Removed) 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.

W Marsh
Guest
Posts: n/a

 05-08-2006
On Mon, 08 May 2006 21:53:59 GMT, Keith Thompson <(E-Mail Removed)>
wrote:

>W Marsh <(E-Mail Removed)> writes:
>> On 8 May 2006 14:03:08 -0700, (E-Mail Removed) 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.

Ian Collins
Guest
Posts: n/a

 05-08-2006
Harald van DÄ³k wrote:
> Ian Collins wrote:
>
>>(E-Mail Removed) 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.