Jeegar Patel
Jeegar Patel

Reputation: 27210

little endian to big endian

see

i have already written one library (on little endian machine)it works fine in little endian machine now i when i run in in big endian platform it doesn't works .error are very hard to understand. Now i have understood the concept of endianess but still i am not getting...

i want to know for making ma library for `big-endian` which changes should i 
take care in ma code? 

i wan to know which operation does have different behaviour in both endian

Upvotes: 1

Views: 1689

Answers (3)

Secure
Secure

Reputation: 4368

To specify the issue cnicutar mentions, a typical candidate for issues is when you directly access parts of a type by an array of a different type, instead of using calculations for access.

unsigned long int  a = 0x04030201ul;

/* Directly accesses the representation, gives 2 on LE and 3 on BE */
b = ((unsigned char *)&a)[1];

/* Works with the values, always gives 2 */
b = (a >> 8) & 0xff;

Upvotes: 1

trenki
trenki

Reputation: 7363

Is your library using binary data files?

When using binary files you have to take care in which format (big vs. little endian) you are writing/reading your data. For instance when writing an array of integers to a file they will be stored in the endianess of the machine that does the writing. When reading you have to take this circumstance into account and convert the data if necessary.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182609

Lots of things might need to be changed (it's difficult to give a comprehensive list: "this is what could go wrong").

Generally endianness issues arise when one tries to access directly the contents of the memory of an integer (say with memcpy for example, union tricks etc).

Upvotes: 6

Related Questions