更新时间:2022-02-05 05:46:53
您只需要将unsigned char
强制转换为char
,因为string
类没有接受unsigned char
的构造函数:
You just needed to cast the unsigned char
into a char
as the string
class doesn't have a constructor that accepts unsigned char
:
unsigned char* uc;
std::string s( reinterpret_cast< char const* >(uc) ) ;
但是,如果您的字节数组包含空值,则需要在构造函数中使用length参数,就好像您不包含该数组一样,只有部分数组会以字符串结尾(该数组直到第一个空值)
However, you will need to use the length argument in the constructor if your byte array contains nulls, as if you don't, only part of the array will end up in the string (the array up to the first null)
size_t len;
unsigned char* uc;
std::string s( reinterpret_cast<char const*>(uc), len ) ;