ASL
Loading...
Searching...
No Matches
Binary data

Detailed Description

Reading and writing binary data as a stream (in big or little endian order).

Files can be read or written as binary streams with the File class in specific endianness (byte order).

File file("data.bin", File::WRITE);
file.setEndian(ENDIAN_LITTLE);
file << 3 << 0.5; // writes a 32-bit int and an 64-bit double (12 bytes total) in little endian byte order.

Using class TextFile instead woud write those variables as text (as "30.5" because we did not add whitespace separation).

TCP sockets (class Socket) can also be read or written as binary streams:

Socket socket;
socket.connect("someserver", 9000);
socket.setEndian(ENDIAN_BIG);
socket << 3 << 0.5;
int32_t x, y;
socket >> x >> y;

That will call an actual socket read or write operation for each variable. Alternatively we can use binary buffers and read or write to them as streams with classes StreamBuffer and StreamBufferReader.

StreamBuffer buffer(ENDIAN_BIG);
buffer << 3 << 0.5; // fill binary buffer
socket << *buffer; // send it all at once
ByteArray data(12);
socket.read(data.data(), 12); // read 12 bytes into a buffer
StreamBufferReader reader(ENDIAN_BIG);
int32_t x, y;
reader >> x >> y; // read 2 int32 variables from the buffer
Array< byte > ByteArray
An alias for Array<byte>
Definition Array.h:618

Classes

class  StreamBufferReader
 This class allows reading a memory buffer as a binary stream. More...
 
class  StreamBuffer
 This class is a buffer that can be written to as a binary stream. More...