我有一个unsigned char数组,我需要一个std :: string,但我目前的方式使用reinterpret_cast,我想避免。有没有更干净的方法来做到这一点?如何将unsigned char转换为不带reinterpret_cast的std :: string?
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' ...? – egrunin 2010-05-15 16:40:59
对于强制演员来说,它总是看起来像是黑客,我宁愿按照现状使用,而不是强迫编译器把它们当作别的东西。 – WilliamKF 2010-05-15 17:43:01
为什么不用'char'作为'my_txt';毕竟,你发布的这些值是ASCii。这可能会导致解决其他问题。 – 2010-05-15 19:04:26