473,761 Members | 6,563 Online
Bytes | Software Development & Data Engineering Community
+ 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,lit tle);

}

Dec 14 '06 #1
13 27302
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(uns igned int x)
{
return
(x>>24) |
((x>>8) & 0x0000ff00) |
((x<<8) & 0x00ff0000) |
(x<<24);
}

Cheers,
Loic.

Dec 14 '06 #2
<ju**********@y ahoo.co.inwrote in message
news:11******** **************@ 80g2000cwy.goog legroups.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,lit tle);

}
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(uns igned 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**********@m urdoch.hpl.hp.c om>,
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**********@m urdoch.hpl.hp.c om>,
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**********@y ahoo.co.in" <ju**********@y ahoo.co.inwrite s:
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,lit tle);

}
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_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.
Dec 14 '06 #9
"Anoop Saxena" <an**********@g mail.comwrote in message
news:11******** *************@t 46g2000cwa.goog legroups.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

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

Similar topics

2
29013
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 under linux debian 3.0 woody Thank you for your help.
0
2385
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 endian). So I have to change the order of the bytes for "int". But for "float":
0
1630
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, sometimes, the application stop working. In the moment that application fails, the only way to get it working again is to restart WebSphere server or JDBC pool.
2
9774
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
3177
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 without compression). It has it's words and dwords stored in little- endian, so I do a conversion to big-endian when reading full words or dwords. I have done this because my system is big-endian. But now... what if one compiles the library in a...
23
7048
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."); }
0
9336
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
10111
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
9948
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...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6603
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
5215
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...
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.