简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM