Advertisements

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, |

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.

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.

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.

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.

Harald van DÄ³k 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.

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.

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.

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.

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.

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

<> 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 ?
>

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.

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 ?

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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!