Istream and ostream through platforms

Suppose I would like to write this on my machine with great enthusiasm

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

And this is in my little car

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

Is there an istream / ostream implementation that allows this? For instance. which always transfers numbers in Big Endian (or in any format)?

+4
source share
3 answers

I do not know about any implementation capable of this ready-made box. The easiest way to write something like this is probably possible with Boost.Iostreams . You can simply implement the devices for the respective conversions, and then use stream to create a stream that delegates to the device you wrote and provides a standard iostream interface.

+1
source

If you need to exchange data more complex than a single unit between machines, I would heartily recommend Google Protocol Buffers . You define the "messages" in a text file, run them through the compiler (protoc) and get the source in your chosen language. This source defines objects that match your message specifications, as well as serialization and deserialization methods.

+2
source

In this case, the only way is probably to serialize / deserialize itself, i.e. read / write byte by byte.

0
source

Source: https://habr.com/ru/post/1379630/


All Articles