C++中 unsigned char*不能强制转换为string类型
如果unsigned char*中包含’\0’字符,可以通过以下方式进行转换以不丢失数据
unsigned char szSrc[] = {0x32, 0x00, 0x00, 0x99, 0x34, 0x00, 0x62};
int ilen = sizeof(szSrc)/sizeof(szSrc[0]);
string sdes = "";
for (size_t i = 0; i < ilen; i++)
sdes += szDest[i];
如果是先把unsigned char* 类型强制转换为char* 类型,再强制转换为string类型。因为在转换为char*类型的途中遇空(0x00)终止,如果字符串中含有0x00字符,会导致赋值给string的数据与源数据不一致。