我有一个unsigned char数组,需要转换为std::string,但是我当前的方法使用了reinterpret_cast,我想避免这种方式。 有没有更简洁的方法?
unsigned char my_txt[] = {
0x52, 0x5f, 0x73, 0x68, 0x7e, 0x29, 0x33, 0x74, 0x74, 0x73, 0x72, 0x55
}
unsigned int my_txt_len = 12;
std::string my_std_string(reinterpret_cast<const char *>(my_txt), my_txt_len);
reinterpret_cast
呢? - egruninmy_txt
中使用char
呢?毕竟,你发布的这些值是 ASCii 码。这可能会有助于解决其他问题。 - Thomas Matthewsunsigned char*
初始化一个string
对我来说似乎很合理。我很惊讶有人反对不使用强制转换就想这样做。 - Kyle Strand