473,466 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

converting a big endian to little endian

Hi guys,

I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.

#include <stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char *big_ptr = (char *)&big;
char *little_ptr = (char *)&little;

little_ptr[0] = big_ptr[3];
little_ptr[1] = big_ptr[2];
little_ptr[2] = big_ptr[1];
little_ptr[3] = big_ptr[0];

printf("big = 0x%x little = 0x%x\n",big,little);

}

Dec 14 '06 #1
13 27212
Hello,
I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.
You can use 'shift' combined with 'and' operator to get the same
result:

/*
* convert big -little endian and conversly
* this function assumes that sizeof(unsigned int) == 4
*/
unsigned int
endian_swap(unsigned int x)
{
return
(x>>24) |
((x>>8) & 0x0000ff00) |
((x<<8) & 0x00ff0000) |
(x<<24);
}

Cheers,
Loic.

Dec 14 '06 #2
<ju**********@yahoo.co.inwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
Hi guys,

I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.

#include <stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char *big_ptr = (char *)&big;
char *little_ptr = (char *)&little;

little_ptr[0] = big_ptr[3];
little_ptr[1] = big_ptr[2];
little_ptr[2] = big_ptr[1];
little_ptr[3] = big_ptr[0];

printf("big = 0x%x little = 0x%x\n",big,little);

}
The code above should work fine. However, the more traditional approach is
to use a union, i.e.

union
{
int i;
char c[4];
} pickapart;

pickapart.i = input_arg;
output0 = pickapart.c[0];

However, if you can find an approach involving shifting only, and if an
examination of the assembly-language shows that the compiler does it well
(probably without shifting), that would be the preferred approach, i.e.

output0 = input_arg & 0xFF;
output1 = (input_arg >>8) & 0xFF;
etc.

then that would be the preferred approach. The approach you cited can lead
to memory addressing problems if the int size is <32, and the approach I
cited with the union won't work for other sizes of integers. There should
be a more portable and general approach.

Dec 14 '06 #3
Hi,
New here, so apologies if I have not followed any rules while
adding code snippets etc. Do let me know.
endian_swap(unsigned int x)
{
return
(x>>24) |
((x>>8) & 0x0000ff00) |
((x<<8) & 0x00ff0000) |
(x<<24);
}

Cheers,
Loic.
If int is 32 bits, then how good is the following code for unsigned
int's?

int main(void)
{
unsigned int x = 0xffaa2211;
unsigned int z = 0;
z = ( (x << 16) | ( x >16) );

return 0;
}

Dec 14 '06 #4
Anoop Saxena wrote:
If int is 32 bits, then how good is the following code for unsigned
int's?

int main(void)
{
unsigned int x = 0xffaa2211;
unsigned int z = 0;
z = ( (x << 16) | ( x >16) );

return 0;
}
Broken. It will generate 0x2211ffaa, not the required
0x1122aaff.

(It also has /definitely/ superfluous brackets and an
unnecessary initialisation of `z`, which are style issues,
and it would have been nice for there to be some kind
of output.)

--
Chris "Perikles triumphant" Dollin
Scoring, bah. If I want scoring I'll go play /Age of Steam/.

Dec 14 '06 #5
2006-12-14 <el**********@murdoch.hpl.hp.com>,
Chris Dollin wrote:
Anoop Saxena wrote:
> z = ( (x << 16) | ( x >16) );

(It also has /definitely/ superfluous brackets
Precedence rules for bitwise operators are not widely understood.
Dec 14 '06 #6
Random832 wrote:
2006-12-14 <el**********@murdoch.hpl.hp.com>,
Chris Dollin wrote:
>Anoop Saxena wrote:
>> z = ( (x << 16) | ( x >16) );

(It also has /definitely/ superfluous brackets

Precedence rules for bitwise operators are not widely understood.
I don't doubt it, but those aren't the ones I was talking about.

--
Chris "Perikles triumphant" Dollin
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

Dec 14 '06 #7
Broken. It will generate 0x2211ffaa, not the required
0x1122aaff.
Thanks. Stupid bug. :(

Anoop.

Dec 14 '06 #8
"ju**********@yahoo.co.in" <ju**********@yahoo.co.inwrites:
I need to convert a big endian integer to little endian integer.
(the integer is 4 bytes in size on my implementation). I came up with
the following code. I need your comments on this. Please suggest any
improvements that can be done.

#include <stdio.h>
int main(void)
{
int big = 0x12345678;
int little;
char *big_ptr = (char *)&big;
char *little_ptr = (char *)&little;

little_ptr[0] = big_ptr[3];
little_ptr[1] = big_ptr[2];
little_ptr[2] = big_ptr[1];
little_ptr[3] = big_ptr[0];

printf("big = 0x%x little = 0x%x\n",big,little);

}
I'd use unsigned char rather than plain char. It's not likely to make
any real difference, but unsigned char is generally better for
arbitrary binary data.

--
Keith Thompson (The_Other_Keith) 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.
Dec 14 '06 #9
"Anoop Saxena" <an**********@gmail.comwrote in message
news:11*********************@t46g2000cwa.googlegro ups.com...
If int is 32 bits, then how good is the following code for unsigned
int's?

int main(void)
{
unsigned int x = 0xffaa2211;
unsigned int z = 0;
z = ( (x << 16) | ( x >16) );

return 0;
}
That converts little-endian to/from PDP-endian, not big-endian.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Dec 14 '06 #10
2006-12-14 <el**********@murdoch.hpl.hp.com>,
Chris Dollin wrote:
Random832 wrote:
>2006-12-14 <el**********@murdoch.hpl.hp.com>,
Chris Dollin wrote:
>>Anoop Saxena wrote:
z = ( (x << 16) | ( x >16) );

(It also has /definitely/ superfluous brackets

Precedence rules for bitwise operators are not widely understood.

I don't doubt it, but those aren't the ones I was talking about.
Some people might think that without the ones you were talking about it
would have been the equivalent of (z=(x<<16))|(x>>16). The confusion is
that widespread.
Dec 15 '06 #11
>
That converts little-endian to/from PDP-endian, not big-endian.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein

Absolutely my mistake. I got it mixed with my response to the following
thread in another C forum and mixed the posts. I apologize.
"
hi all.

Here is an interesting problem hope i will get a solution of it.

We all no the sizeof int depends on the hardware..Somewhere it is
2bytes and somewhere it is 4 bytes. Now i would like to present a
interesting problem based on this important property.

1)First you have to find out what is the size of the int in the
platform.
2) now suppose you got 2 bytes or 4 bytes... so in case of 2 bytes you
can represent a int no as int x = 0xa1a2; and in 4bytes you can
represent it as int x = a1a2a3a4; so now in case of 2 byte you have to
change the byte order i.i x = a1a2 should become x = a2a1 and in case
of 4bytes x = 0xa1a2a3a4 should become x = 0xa3a4a1a2.

hope you can provide with the solution. "

Dec 15 '06 #12
Random832 wrote:
2006-12-14 <el**********@murdoch.hpl.hp.com>,
Chris Dollin wrote:
>Random832 wrote:
>>2006-12-14 <el**********@murdoch.hpl.hp.com>,
Chris Dollin wrote:
Anoop Saxena wrote:
z = ( (x << 16) | ( x >16) );

(It also has /definitely/ superfluous brackets

Precedence rules for bitwise operators are not widely understood.

I don't doubt it, but those aren't the ones I was talking about.

Some people might think that without the ones you were talking about it
would have been the equivalent of (z=(x<<16))|(x>>16). The confusion is
that widespread.
I find that hard to believe, although if that's what you've seen,
that's what you've seen. (If "some" is small enough I'd put it
under "some people can misunderstand /anything/" ...)

--
Chris "Perikles triumphant" Dollin
The shortcuts are all full of people using them.

Dec 15 '06 #13
2006-12-15 <el**********@murdoch.hpl.hp.com>,
Chris Dollin wrote:
Random832 wrote:
>2006-12-14 <el**********@murdoch.hpl.hp.com>,
Chris Dollin wrote:
>>Random832 wrote:

2006-12-14 <el**********@murdoch.hpl.hp.com>,
Chris Dollin wrote:
Anoop Saxena wrote:
> z = ( (x << 16) | ( x >16) );
>
(It also has /definitely/ superfluous brackets

Precedence rules for bitwise operators are not widely understood.

I don't doubt it, but those aren't the ones I was talking about.

Some people might think that without the ones you were talking about it
would have been the equivalent of (z=(x<<16))|(x>>16). The confusion is
that widespread.

I find that hard to believe, although if that's what you've seen,
that's what you've seen. (If "some" is small enough I'd put it
under "some people can misunderstand /anything/" ...)
In that case it would be not so much that bitwise operators are
confusing as it is that they're _intimidating_.
Dec 15 '06 #14

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

Similar topics

2
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
0
by: Ernst Murnleitner | last post by:
Dear readers, Maybe someone can help: I need to exchange data with a PLC. My computer (Linux on intel) uses the intel format (little endian), the other device uses the motorola format (big...
0
by: ClimberBear | last post by:
Hi, I've got a very strange problem with a Websphere 5.1 cluster attached to DB2 database in Mainframe z/OS. I have a J2EE deployed application running normally fine agains the DB2 host. But,...
2
by: Mehta Shailendrakumar | last post by:
Hi, I am sending this question again as new question rather than reply to old question Please refer below: struct raw_data { unsigned char x; unsigned char y; }; union full_data
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
23
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.