Hal Vaughan wrote:
> I have a series of codes that will include NULL bytes and other
> non-text data that I need to send to a device through a file.
> Since a NULL terminates a string
Nope. But there is the concept of "Null-Terminated Byte Strings" (NTBS):
many functions treat a '\0' as the end of the string.
> Do I have to handle the string (or char[]) in a special way to make
> sure all the data is kept intact?
You must not use any of these NTBS functions, that's all.
In C, an (un)signed char[] should be fine, and ordinary array
initialization will work as expected:
unsigned char dev_out[6] = { '\0x03', '\0', 0, 255, 'x', '\r' };
I'm not aware of any sup****t for NUL in string literals, e.g.
"abc\0def"; I'd just avoid it.
Of course, you cannot use strlen() or strcpy() on such objects.
In C++, a std::string _can_ handle embedded NUL characters, but there
are also many convenient functions for NUL-terminated strings, which are
useless and dangerous for your purpose. And as you said you have "a
series of codes", using a std::string just sounds unnatural to me.
A std::vector<unsigned char> is probably a better choice, and won't
cause you any trouble.
Last not least, make sure to open the device in binary mode, and use
only appropriate output functions.
-- ralph


|