Why do char arrays stop right before a 0x00 byte is detected and how can this problem be avoided (perhaps by using another datatype (which one and why) or a “trick” with char)?
For example in the following code, the output is “a” only, the other bytes are not displayed:
unsigned char cbuffer[]={0x61,0x00,0x62,0x63,0x0};
std::string sbuffer=reinterpret_cast<const char*>(cbuffer);
cout << sbuffer << endl;
Similarly in the following code, the output is “ab”:
unsigned char cbuffer[]={0x61,0x62,0x00,0x63,0x0};
std::string sbuffer=reinterpret_cast<const char*>(cbuffer);
Straightforward and effective workarounds to the problem (where 0x00 is kept in the array as a normal byte) would be appreciated.
It’s common in C to pass around strings as pointers to null-terminated
chararrays. null is represented by0x00. To make conversion easy, thestd::stringis constructable from a pointer to a null-terminated char array, which is what is happening with your code. But when it finds the null, it thinks that’s the end of the string. If youcouta char array directly, you’ll find it makes the same assumption, because they have no other way to determine the end of a string pointed to by achar*. (They could theoretically tell the length in your case, if they understoodchar (&)[], but almost nothing in the standard library does sadly).The intended workarounds are to use this constructor instead:
or
However, you have to be careful with
sizeof(cbuffer). Ifcbufferis achar*(pointer) instead of achar(&)[](array), thensizeof(ptr)will return the wrong value, and there is no way to get the correct length at that point, if the string is not null-terminated.