Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   swapping bytes in an integer (http://www.velocityreviews.com/forums/t442657-swapping-bytes-in-an-integer.html)

 john@fcs.uga.edu 05-08-2006 09:03 PM

swapping bytes in an integer

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 05-08-2006 09:11 PM

Re: swapping bytes in an integer

On 8 May 2006 14:03:08 -0700, john@fcs.uga.edu 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 05-08-2006 09:16 PM

Re: swapping bytes in an integer

john@fcs.uga.edu 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 05-08-2006 09:17 PM

Re: swapping bytes in an integer

john@fcs.uga.edu 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 05-08-2006 09:35 PM

Re: swapping bytes in an integer

> Monkey monkey = 0x090a0b0c;

Opps! Should have written:

Monkey monkey;

monkey.four_bytes = 0x090a0b0c;

-Tomás

 W Marsh 05-08-2006 09:37 PM

Re: swapping bytes in an integer

On Mon, 08 May 2006 21:35:03 GMT, "Tomás" <NULL@NULL.NULL> 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 05-08-2006 09:53 PM

Re: swapping bytes in an integer

W Marsh <wayne.marsh@gmail.com> writes:
> On 8 May 2006 14:03:08 -0700, john@fcs.uga.edu 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) kst-u@mib.org <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?= 05-08-2006 09:54 PM

Re: swapping bytes in an integer

Ian Collins wrote:
> john@fcs.uga.edu 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 05-08-2006 10:02 PM

Re: swapping bytes in an integer

On Mon, 08 May 2006 21:53:59 GMT, Keith Thompson <kst-u@mib.org>
wrote:

>W Marsh <wayne.marsh@gmail.com> writes:
>> On 8 May 2006 14:03:08 -0700, john@fcs.uga.edu 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 05-08-2006 10:05 PM

Re: swapping bytes in an integer

Harald van DÄ³k wrote:
> Ian Collins wrote:
>
>>john@fcs.uga.edu 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.

All times are GMT. The time now is 07:37 AM.