Click here to Skip to main content
Click here to Skip to main content

Endian-ness problems

By , 25 May 2006
Rate this:
Please Sign up or sign in to vote.

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.


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


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

About the Author

Boby Thomas P
Software Developer (Senior) DWS
Australia Australia

Comments and Discussions

 
GeneralLets say we know the issue... PinmemberJun Du25-May-06 6:07 
GeneralRe: Lets say we know the issue... PinmemberTeashirt225-May-06 7:59 
GeneralRe: Lets say we know the issue... PinmemberBoby Thomas P25-May-06 8:42 
GeneralRe: Lets say we know the issue... PinmemberTeashirt225-May-06 8:49 
GeneralRe: Lets say we know the issue... [modified] PinmemberBoby Thomas P25-May-06 8:52 
GeneralRe: Lets say we know the issue... PinmemberEnnis Ray Lynch, Jr.25-May-06 9:34 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 25 May 2006
Article Copyright 2006 by Boby Thomas P
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid