11,704,315 members (60,874 online)

# Endian-ness problems

, 25 May 2006 25.2K 227 14
 Rate this:
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.

 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.

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

 Software Developer (Senior) DWS Australia

## Comments and Discussions

 First Prev Next
 Lets say we know the issue... Jun Du25-May-06 6:07 Jun Du 25-May-06 6:07
 Re: Lets say we know the issue... Teashirt225-May-06 7:59 Teashirt2 25-May-06 7:59
 Re: Lets say we know the issue... Boby Thomas P25-May-06 8:42 Boby Thomas P 25-May-06 8:42
 Re: Lets say we know the issue... Teashirt225-May-06 8:49 Teashirt2 25-May-06 8:49
 Re: Lets say we know the issue... [modified] Boby Thomas P25-May-06 8:52 Boby Thomas P 25-May-06 8:52
 Re: Lets say we know the issue... Ennis Ray Lynch, Jr.25-May-06 9:34 Ennis Ray Lynch, Jr. 25-May-06 9:34
 Last Visit: 31-Dec-99 18:00     Last Update: 28-Aug-15 12:07 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

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