xtofl
xtofl

Reputation: 41519

istream and ostream across platforms

Suppose I would want to write this on my big-endian machine

an_ostream_impl my_output_on_BE;
my_output_on_BE << __int32(0x1234);

And this on my little-endian machine

an_istream_impl my_input_on_LE;  

__int32 value;
my_input_on_LE >> value;
assert( value == 0x1234 );

Is there an istream/ostream implementation allowing this? E.g. that always streams numbers in Big Endian (or whatever format)?

Upvotes: 4

Views: 1062

Answers (3)

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76886

I don't know of any implementation that is capable of this out-of-the-box. The easiest way to write something like this yourself is probably by using Boost.Iostreams. You could simply implement devices for the appropriate conversions, and then use stream to create a stream that delegates to the device you have written and exposes a standard iostream interface.

Upvotes: 1

RobH
RobH

Reputation: 3348

If you need to share data more complicated than a single integer between machines I'd heartily recommend Google Protocol Buffers. You define "messages" in a text file, run them through a compiler (protoc) and get source in your language of choice. This source defines objects corresponding to your message specifications plus serialisation and deserialisation methods.

Upvotes: 2

AndersK
AndersK

Reputation: 36092

In that case the only way is probably to serialize/deserialize yourself i.e. reading/writing byte by byte.

Upvotes: 0

Related Questions