【问题标题】:Convert const Byte* to std::string c++将 const Byte* 转换为 std::string c++
【发布时间】:2013-03-13 18:39:11
【问题描述】:

如何将 const Byte* 转换为 std::string。我尝试了以下

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

其中 ByteBuffer 是 const Byte*。使用上面的代码没有得到 sTemp 中 ByteBuffer 的值 请帮忙

谢谢,

【问题讨论】:

  • 什么是Byte? “没有得到价值”是什么意思?

标签: c++ visual-c++


【解决方案1】:

如果ByteBuffer 包含一个以 NUL 结尾的字符串,那么代码应该可以正常工作。

如果没有,您需要将其长度作为第二个参数提供给string constructor

以上假设Byte 是我认为的,而ByteBuffer 不是NULL

【讨论】:

    【解决方案2】:

    C++ 中没有“字节”这样的数据类型。如果它存在的话,我假设它是一个 8 位数据存储,在这种情况下你的代码是正确的。确保缓冲区中有一个空字符 - '\0'。

    【讨论】:

    • BYTE,另一方面,是由 Windows 定义的。
    • 另外需要注意的是,BYTE 很可能是unsigned char
    猜你喜欢
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多