473,769 Members | 2,234 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
13 27305
2006-12-14 <el**********@m urdoch.hpl.hp.c om>,
Chris Dollin wrote:
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.
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..Somew here 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**********@m urdoch.hpl.hp.c om>,
Chris Dollin wrote:
>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.

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**********@m urdoch.hpl.hp.c om>,
Chris Dollin wrote:
Random832 wrote:
>2006-12-14 <el**********@m urdoch.hpl.hp.c om>,
Chris Dollin wrote:
>>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.

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
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
2386
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
1631
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
9776
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
3178
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
7050
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
9590
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
10223
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
10051
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
8879
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...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
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

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.