I have a uint8_t vector which contains cbor bytes and I have to cast or copy the bytes to a const char * to use in a POSIX message queue. I've tried the following approaches
vector<uint8_t> cbor_data{/*has some data*/};
const char *data = (const char *)(cbor_data.data());
and
vector<uint8_t> cbor_data{/*has some data*/};
char* data = new char[cbor_data.size() + 1];
memcpy(data, cbor_data.data(), cbor_data.size());
data[cbor_data.size()] = '\0';
But only a part of the data gets cast or copied. Both methods seem to fail. I have a few bytes whose value is 0. For example this is what I have in the vector:
a1,69,72,65,6d,69,6e,64,65,72,73,88,a5,66,63,6f,6e,66,69,67,a5,67,65,6e,61,62,6c,65,64,f4,72,72,65,6d,69,
6e,64,65,72,45,78,70,69,72,79,54,79,70,65,72,73,79,73,74,65,6d,43,61,6c,65,6e,64,61,72,54,69,6d,65,6c,72,
65,6d,69,6e,64,65,72,54,69,6d,65,00,66,76,69,65,77,65,64,f4,6b,77,72,69,74,65,41,63,63,65,73,73,66,72,65,
6d,6f,74,65,62,69,64,00,69,70,75,62,6c,69,73,68,65,72,a2,6c,64,6f,4e,6f,74,50,65,72,73,69,73,74,f5,6d,70
and this is what I am left with when I print data:
a1,69,72,65,6d,69,6e,64,65,72,73,88,a5,66,63,6f,6e,66,69,67,a5,67,65,6e,61,62,6c,65,64,f4,72,72,65,6d,69,
6e,64,65,72,45,78,70,69,72,79,54,79,70,65,72,73,79,73,74,65,6d,43,61,6c,65,6e,64,61,72,54,69,6d,65,6c,72,
65,6d,69,6e,64,65,72,54,69,6d,65,00
Everything after the 00 seems to be lost. Its not just while printing data. strlen(data) and cbor_data.size() dont match. Is there another way to go about this? Or a better way to pass a vector of char/uint8_t to a posix message queue?