Neppomuk
Neppomuk

Reputation: 1210

How do I feed a byte array with null characters into a std::iostream?

I have to feed a byte array, which contains null characters, into a derivative of std::iostream. The raw data looks as follows:

FF 73 00 05 73

I stored this chunk of data into a char array:

char *tmc_command = new char [5];  // array size may vary
SerialStream tmc_receiver_handler; // a derivative of std::iostream

tmc_command [1] = 0xFF;
tmc_command [1] = 0x73;
tmc_command [2] = 0x00; // NULL character, eeeeeehhhh!!! :-(
tmc_command [3] = 0x05;
tmc_command [4] = 0x73;

instance -> tmc_receiver_handler << tmc_command;

When doing this, unfortunately, output stops at position 2 because of the null character.

As this is a mere byte array and not a real string, how can I make the stream object simply spit out the complete tmc_command without stopping at the null character? Or should I use a different object instead of the char []? Thank you.

Upvotes: 0

Views: 688

Answers (2)

user13708060
user13708060

Reputation: 11

I would check out the write function -- reference here http://www.cplusplus.com/reference/ostream/ostream/write/

Upvotes: 1

Stephen M. Webb
Stephen M. Webb

Reputation: 1869

The ostream inserter operators are for formatting. If you do not want to print your data as a formatted string, the formatted insertion operator is not what you want.

Try using the ostream write() member function instead.

Upvotes: 1

Related Questions