Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to read big endian data with little endian program?

Tags:

c++

endianness

An external group provides me with a file written on a Big Endian machine, and they also provide a C++ parser for the file format.

I only can run the parser on a little endian machine - is there any way to read the file using their parser without add a swapbytes() call after each read?

like image 780
Brian Avatar asked Jan 07 '10 18:01

Brian


1 Answers

Back in the early Iron Age, the Ancients encountered this issue when they tried to network primitive PDP-11 minicomputers with other primitive computers. The PDP-11 was the first little-Endian computer, while most others at the time were big-Endian.

To solve the problem, once and for all, they developed the network byte order concept (always big-Endia), and the corresponding network byte order macros ntohs(), ntohl(), htons(), and htonl(). Code written with those macros will always "get the right answer".

Lean on your external supplier to use the macros in their code, and the file they supply you will always be big-Endian, even if they switch to a little-Endian machine. Rewrite the parser they gave you to use the macros, and you will always be able to read their file, even if you switch to a big-Endian machine.

A truly prodigious amount of programmer time has been wasted on this particular problem. There are days when I think a good argument could be made for hanging the PDP-11 designer who made the little-Endian feature decision.

like image 118
John R. Strohm Avatar answered Sep 27 '22 21:09

John R. Strohm