简体   繁体   English

将const Byte *转换为std :: string c ++

[英]Convert const Byte* to std::string c++

How can i convert const Byte* to std::string.I tried the following 如何将const Byte *转换为std :: string。我尝试了以下方法

std::string sTemp(reinterpret_cast<const char*>(ByteBuffer));

Where ByteBuffer is const Byte*.Not got the value of ByteBuffer in sTemp using the above code Please help 其中ByteBuffer是const Byte *。没有使用上面的代码在sTemp中获得ByteBuffer的值请帮助

Thanks, 谢谢,

If ByteBuffer contains a NUL-terminated string, then the code should work fine. 如果ByteBuffer包含一个以NUL结尾的字符串,那么代码应该可以正常工作。

If it doesn't, you need to supply its length as the second parameter to the string constructor . 如果没有,则需要将其长度作为string构造函数的第二个参数提供。

The above assumes that Byte is what I think it is, and that ByteBuffer is not NULL . 以上假设Byte是我认为的,并且ByteBuffer不是NULL

There's no such data type as 'Byte' in C++. 在C ++中没有像'Byte'这样的数据类型。 I assume it's a 8-bit data store if at all it exists, in which case your code is correct. 我认为它是一个8位数据存储,如果它存在,在这种情况下你的代码是正确的。 Make sure there's a null character - '\\0' in the buffer. 确保缓冲区中有一个空字符 - '\\ 0'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM