Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

endian independent file io in C++

Name: Anonymous 2007-02-24 21:45 ID:XOqzVx8R

What is the correct way to do this?  I'm doing:

data->put (x % 256);
data->put (x / 256);

and

x = data->get ();
x += data->get () * 256;

But I'm sure there's a better way.  Also, this way doesn't work with negative numbers.

Name: Anonymous 2007-02-24 23:11 ID:W7X2Ljhb

>>3 is an EXPERT PROGRAMMER.

Use the API of your choice to automatically translate the native endian-ness of the machine that the code is running on to the machine-independent network byte order.  If all persisted data is stored in network byte order, the endian nature of the machine reading or writing the file becomes irrelevant.

(This is a classic technique taught in any self-respecting OpSys course.)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List