C++ 标准::向量<;乌查尔>;数据丢失

C++ 标准::向量<;乌查尔>;数据丢失,c++,vector,casting,type-conversion,C++,Vector,Casting,Type Conversion,我使用cv::imencode将cv::Mat作为image/jpeg编码为向量,现在我想将该向量转换为char*类型 vector<uchar> buf; // print buf to stdout to ensure that data is valid here for (auto c : buf) cout << c << endl; // cast vector to char char *ch = reinterpret_cast&l

我使用
cv::imencode
cv::Mat
作为
image/jpeg
编码为
向量
,现在我想将该向量转换为
char*
类型

vector<uchar> buf;

// print buf to stdout to ensure that data is valid here
for (auto c : buf)
    cout << c << endl;

// cast vector to char
char *ch = reinterpret_cast<char*>(buf.data());

// print out value of char pointer
for(int i = 0; ch[i] != '\0'; i++)
    printf("log: %c\n", ch[i]);
vectorbuf;
//将buf打印到标准输出以确保此处的数据有效
用于(自动c:buf)

cout处理向量时,一切都很好,因为向量是一个具有显式大小的动态数组。因此它可以包含空值

但接下来,您将使用以null结尾的无符号字符数组。所以它在第一个空字符处停止。它甚至在代码中是显式的

for(int i = 0; ch[i] != '\0'; i++)
    printf("log: %c\n", ch[i]);
(相关部分为
ch[i]!=0


这就是为什么在第一个空字符之后会丢失所有字符。

确定缓冲区中只有可打印字符吗?为什么强制转换为
char*
而不是
无符号char*
buf
中有什么类型的数据?你到底想做什么?或者这只是一个有趣的问题?这是一个模块的一部分,该模块对图像进行处理。因此,
buf
将此处理后的图像包含为jpeg。然而,我认为粘贴完整的文件会分散对实际问题的注意力。@ilent2我需要转换为
char*
,因为cgo无法导出
未签名的char*
。如果
buf
包含
0x00
的实例,那么将第二个循环的结束条件更改为
I
怎么样?