Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > swapping bytes in an integer

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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, |
 
Reply With Quote
 
 
 
 
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
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
 
Reply With Quote
 
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.
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-08-2006

> Monkey monkey = 0x090a0b0c;



Opps! Should have written:


Monkey monkey;

monkey.four_bytes = 0x090a0b0c;


-Tomás
 
Reply With Quote
 
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.
 
Reply With Quote
 
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.
 
Reply With Quote
 
=?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.

 
Reply With Quote
 
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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-08-2006
Harald van Dijk 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
4-bytes or 8-bytes alignment? mrby C Programming 8 11-02-2004 08:45 PM
Private Bytes vs. # Bytes in all Heaps in Perfmon Jason Collins ASP .Net 3 02-18-2004 03:59 PM
Re: receiving Bytes and sending Bytes Ieuan Adams Computer Support 0 07-24-2003 07:46 PM
Re: receiving Bytes and sending Bytes The Old Sourdough Computer Support 0 07-23-2003 01:23 PM



Advertisments