Click here to Skip to main content
15,881,881 members
Articles / Programming Languages / C++
Article

Endian-ness problems

Rate me:
Please Sign up or sign in to vote.
1.60/5 (10 votes)
25 May 20062 min read 31.9K   14   6
Problems when we switch between little endian and big endian platform.

Introduction

This article will address the problems we face, when we switch between little endian and big endian platforms.

External Binary files

If we store binary data in an external file in one platform and use the same data in another platform (with different endian-ness), then the data read back will not be the same.

Casting Issues

If you type cast one data type to another, the behavior in little endian system and big endian system will be the same.

For example see the following code,

#Make file written by boby Thomas Pazheparampil
#21-5-2006
int iTmp = 1;
if((char)iTmp)
    cout<<"Not zero after casting";
else
    cout<<"Zero after casting";

If we run the above piece of code in a little endian system and a big endian system, the result will be the same.

<v:shapetype id="_x0000_t63" coordsize="21600,21600" o:spt="63" path="wr0,,21600,21600@15@16@17@18l@21@22xe" adj="1350,25920"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="val #0"><v:f eqn="val #1"><v:f eqn="sum 10800 0 #0"><v:f eqn="sum 10800 0 #1"><v:f eqn="atan2 @2 @3"><v:f eqn="sumangle @4 11 0"><v:f eqn="sumangle @4 0 11"><v:f eqn="cos 10800 @4"><v:f eqn="sin 10800 @4"><v:f eqn="cos 10800 @5"><v:f eqn="sin 10800 @5"><v:f eqn="cos 10800 @6"><v:f eqn="sin 10800 @6"><v:f eqn="sum 10800 0 @7"><v:f eqn="sum 10800 0 @8"><v:f eqn="sum 10800 0 @9"><v:f eqn="sum 10800 0 @10"><v:f eqn="sum 10800 0 @11"><v:f eqn="sum 10800 0 @12"><v:f eqn="mod @2 @3 0"><v:f eqn="sum @19 0 10800"><v:f eqn="if @20 #0 @13"><v:f eqn="if @20 #1 @14"><v:path o:connecttype="custom" textboxrect="3163,3163,18437,18437" o:connectlocs="10800,0;3163,3163;0,10800;3163,18437;10800,21600;18437,18437;21600,10800;18437,3163;@21,@22"><v:handles><v:h position="#0,#1"><v:shape id="_x0000_s1026" style="MARGIN-TOP: 4.55pt; Z-INDEX: 1; LEFT: 0px; MARGIN-LEFT: 378pt; WIDTH: 90pt; POSITION: absolute; HEIGHT: 36pt; TEXT-ALIGN: left" type="#_x0000_t63" adj="-10260,35460" fillcolor="yellow"><v:textbox>


0000 0001


In a Big endian system, the data will look like this.

0x11111111 0x11111112 0x11111113 0x11111114

00

00

00

01

In a little endian system, the data will look like this.

0x11111111 0x11111112 0x11111113 0x11111114

01

00

00

00

<v:shape id="_x0000_s1027" style="MARGIN-TOP: 2.05pt; Z-INDEX: 2; LEFT: 0px; MARGIN-LEFT: 117pt; WIDTH: 90pt; POSITION: absolute; HEIGHT: 36pt; TEXT-ALIGN: left" type="#_x0000_t63" adj="-13320,-11340" fillcolor="yellow"><v:textbox>


0000 0001



The casting in the above case (char)iTmp will give you the value true in both the cases. The reason for this is, in both the cases, compiler knows the fact that the data type he is going to cast is integer. That means he has to cast an integer (4 byte long for 32 bit systems) to a character (1 byte long). So he will take least significant byte in both the cases and assign the value to the character variable created. That means the value 1 will be assigned to the character in both cases. No issues in porting the code from one system to another. If you agree with me, look at the code below.

#Make file written by boby Thomas Pazheparampil
#21-5-2006
int iTmp = 1;
if(*(char *)&iTmp)
    cout<<"Not zero after casting";
else
    cout<<"Zero after casting"; 

If we run the above piece of code in a little endian system and a big endian system, the result will be different.

Just watch the operations going on here.

#Make file written by boby Thomas Pazheparampil
#21-5-2006
&iTmp -> Get reference to iTmp. It’s a pointer to the address 0x11111111.
(char *)&iTmp -> Cast the pointer to a pointer of type char. Now we have a character pointer pointing to 0x11111111.
*(char *)&iTmp -> Get the content of the character pointer pointing to 0x11111111.

Now you know, the result will be different in both the platforms. In little endian system, the value will be 01 since the pointer is pointing to the location 0x11111111. In the case of big endian, the pointer is pointing to 0x11111111. But the value is 0 in this case.

Conclusion

If there is casting of one pointer type to another, the behavior will be different in the case of little endian system and big endian system. This can lead to difference in behavior when we move from one platform to another.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) DWS
Australia Australia

Comments and Discussions

 
GeneralLets say we know the issue... Pin
Jun Du25-May-06 6:07
Jun Du25-May-06 6:07 
GeneralRe: Lets say we know the issue... Pin
Teashirt225-May-06 7:59
Teashirt225-May-06 7:59 
GeneralRe: Lets say we know the issue... Pin
Boby Thomas P25-May-06 8:42
Boby Thomas P25-May-06 8:42 
A simple check can be performed at run time to find out the endian-ness of the system and based on that you can decide what to be done.

int iTmp = 1;
if(*(char *)&iTmp)
cout<<"Target is Little endian";


But only issue is if you take code from one platform to another, the possibility of missing such a runtime failure is more.



Regards,
Boby
GeneralRe: Lets say we know the issue... Pin
Teashirt225-May-06 8:49
Teashirt225-May-06 8:49 
GeneralRe: Lets say we know the issue... [modified] Pin
Boby Thomas P25-May-06 8:52
Boby Thomas P25-May-06 8:52 
GeneralRe: Lets say we know the issue... Pin
Ennis Ray Lynch, Jr.25-May-06 9:34
Ennis Ray Lynch, Jr.25-May-06 9:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.