473,899 Members | 4,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

May 8 '06
34 13944
Ian Collins wrote:
jo**@fcs.uga.ed u 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

May 8 '06 #11
On 8 May 2006 15:18:42 -0700, jo**@fcs.uga.ed u wrote:
Ian Collins wrote:
jo**@fcs.uga.ed u 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.
May 8 '06 #12
On 2006-05-08, W Marsh <wa*********@gm ail.com> wrote:
On 8 May 2006 15:18:42 -0700, jo**@fcs.uga.ed u wrote:
Ian Collins wrote:
jo**@fcs.uga.ed u 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.
May 8 '06 #13
On Mon, 08 May 2006 22:41:34 GMT, Andrew Poelstra
<ap*******@loca lhost.localdoma in> wrote:
On 2006-05-08, W Marsh <wa*********@gm ail.com> wrote:
On 8 May 2006 15:18:42 -0700, jo**@fcs.uga.ed u wrote:
Ian Collins wrote:
jo**@fcs.uga.ed u 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.
May 8 '06 #14
> 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 following article should help:
http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Interaction Modeling
Tool

May 10 '06 #15
EventHelix.com 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 following article should help:
http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Interaction Modeling
Tool

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(s hort 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(lo ng 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;
}

May 10 '06 #16

<jo**@fcs.uga.e du> wrote in message
news:11******** *************@i 40g2000cwc.goog legroups.com...
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&0xFF00000 0)>>24)|((a&0x0 0FF0000)>>8)|(( a&0x0000FF00)<< 8)|((a&0x000000 F
F)<<24);

Of course, you can drop any place holder zeros between 0x and FF.
Rod Pemberton
May 10 '06 #17


jo**@fcs.uga.ed u wrote On 05/08/06 18:18,:
Ian Collins wrote:
jo**@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.

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="potenti al 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>

--
Er*********@sun .com

May 10 '06 #18
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

May 10 '06 #19
Eric Sosman <Er*********@su n.com> writes:
jo**@fcs.uga.ed u wrote On 05/08/06 18:18,:
Ian Collins wrote:
jo**@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.
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="potenti al 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_Keit h) ks***@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.
May 10 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2095
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I would also appreciate any general criticism you have to offer. I don't know yet how to write the degradation-path code for browsers that don't support the DOM methods I'm using, so there are some commented-out paths below. If the content...
270
3990
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these things. Specially Microsoft. Here are my favorite puzzles. Don't send me emails asking for the solutions.
27
4262
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just the first 8-bits for example. For me, I think it would be best if I can declare the 32-bits like this: unsigned char bits;
8
1938
by: ranjeet.gupta | last post by:
Dear All I am not able o understand the exact number of bytes allocation done by the two fucntion given below, It is said that the fuction Condition_String1 allocates the 240 bytes while Condition_String2 allocates the 72 bytes, I am not able to get the Artimatic Numbers to staisfy the above Number of bytes allocated,
9
1457
by: storyGerald | last post by:
I knew some ways of swapping two ints without using a temporary variable. Just like: // Method 1: void swap1(int &a, int &b) { int temp = a; a = b; b = a; }
34
4871
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped? (code should work smoothly across different machine.) Thanks, Ann
1
2764
by: spamacon | last post by:
Hello, I have a strange situation using .Net FW 1.1. I want to use Marshal.PtrToStructure to fill the structure below. The first 3 fields get filled correctly: ulStruct describes how big the structure is in bytes (120 bytes, assuming 32-bit IntPtrs), pWmSnapshot gets 0, and usNumWmSnapshots gets 0. The next field, pICView, is a pointer to another structure, and gets filled with some garbage location, which is actually the lower 2 bytes...
11
5495
by: Jiang Nutao | last post by:
Hi, I simplify my problem like below To convert list aa = into How to do it fast? My real list is huge.
8
2054
by: sexauthor | last post by:
I'm converting a VB6 application over that called a 3rd party DLL with specific data structures. The VB6 code defined custom types for those data structures (ie: one with the specific data types, then one as a word array, and one as a byte array, for use in generating CRCs etc) and would LSet one to the other to copy data over. Naturally we can't do that in .NET. I've done lots of reading in the interoperability books and the closest...
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10863
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7201
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5887
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4720
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4300
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.