Hi,
I am trying to read in a file that is written in little Endian and convert it to big Endian. How do I do this?
Thanks
Hi,
I am trying to read in a file that is written in little Endian and convert it to big Endian. How do I do this?
Thanks
Your question is meaningless since you have provided insufficient information.
If your file is a straight text (non-binary) no conversion is needed.
If your file is binary, you need an unambiguous specification of how the data was written. Without such a specification, no conversion is possible.
In particular, the specification will cover whether data in the (binary) file was written as 16-bit, 32-bit, or 64-bit integral types - each of those has different needs for converting endianess. If your file contains a mix of 16-bit, 32-bit, and 64-bit integral data, you need to know unambiguously what the mix is. For example, a conversion will not work if you assume a given block of eight bytes was written as a 64-bit integer, but it was actually written as a pair of 32-bit integers.
If your binary file contains floating point data (written as float, double, or long double) then no conversion is possible.
You write functions readint16be, readint32be, readint64be. Then you use the shift and logical operators to build up the number. So a 16 bit integer would be (ch1 << 8) | ch2. To be fully portable you need to sign extend, but generally it's good enough to use the type of the right size.
The code then works regardless of whether your machine is big or little endian.
I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
Visit my website for lots of associated C programming resources.
https://github.com/MalcolmMcLean