swapping bytes in an integer

J

john

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

W Marsh

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

Flash Gordon

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.
 
I

Ian Collins

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

W Marsh

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.
 
K

Keith Thompson

W Marsh said:
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.
 
G

Guest

Ian said:
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

W Marsh

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.
 
I

Ian Collins

Harald said:
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.
 
J

john

Ian said:
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.

-John
 
W

W Marsh

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.
 
A

Andrew Poelstra

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.
 
W

W Marsh

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.
 
J

john

EventHelix.com said:
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;
}
 
R

Rod Pemberton

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
 
E

Eric Sosman

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

john

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
 
K

Keith Thompson

Eric Sosman said:
<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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top