Is there a quick method to flip endian-ness in C? Different platforms I am using, use big and little endian, and I want to flip everything to big.
Flip from little endian to big endian ?
Collapse
X
-
Tags: None
-
Why?
To expand on my question if you mean you want to use big endian for all data storage no matter what platform is used then that is a mistake. You should let the platform work in it's native endian, anything else will be atrociously in-efficient.
The only things that should have fixed endian are those things that are truely platform independent, that is communication protocols, file formats and similar methods of data transfer.
You can then easily write functions that read and write data out of the file (or protocol etc.) as bytes and combines those bytes into integers etc in a portible manor using the bitwise operators (shifts, ors and ands). -
-
Why?
Because when we display results from an address, they are reversed, depending on the platform.Comment
-
A memory address? An IP address? You are not explaining your problem fully.
Do these platforms communicate with each other or are they sealed pieces of software, i.e. do you have problem of sending data between platforms with different endians or have you managed to write non-portable software and then transferred it to a platform that it doesn't work properly on?Comment
-
It sounds like you're describing some sort of debugger: you type in an address and it prints out the contents of that address. If so, then I don't see why you need to worry about endianness -- simply cast the address to the appropriate pointer type and dereference the pointer.
In my experience, the only times when I needed to worry about endianness were:
1. Reading or writing a data file that might be used by many platforms. In this case it was important to specify the endianness of the data file so each platform could properly access it.
2. Reading or writing a message stream that might be used by many platforms. In this case it was important to specify the endianness of the message so each platform could properly access it.
3. PCI accesses. The PCI standard specifies the endianness of the memory-mapped I/O, so the platform has comply with that endianness even if it does't match the native endianness of the processor.
4. Interpreting panic dump printouts. [Now I'm showing my age -- does anybody still have to deal with these?]
The common thread here is that endianness only needs to be explicitly taken into account when information is passed between platforms that might have different native endianness.Comment
Comment