[英]Convert unsigned char* array to std::string
I'm using this code to convert the unsigned char* (points to an array of 256 values) to std::string:我正在使用此代码将 unsigned char*(指向 256 个值的数组)转换为 std::string:
int ClassA::Func(unsigned char *dataToSend, int sendLength)
{
std::stringstream convertStream;
std::string dataToSendStr = "";
for(int i=0; i<=sendLength; i++) convertStream << dataToSend[i];
while(!convertStream.eof()) convertStream >> dataToSendStr;
...
}
but then I have dataToSendStr in this format:但后来我有这种格式的 dataToSendStr :
dataToSendStr ""
[0] 0x00
[1] 0x00
[2] 0x04
[3] 0xC0
if I now use this value I only get "" and not the important values [0-3]!如果我现在使用这个值,我只会得到 "" 而不是重要的值 [0-3]!
-> need something like: dataToSendStr "000004C0" -> 需要类似:dataToSendStr "000004C0"
Thx for your help!谢谢你的帮助!
Use the IO manipulator std::hex if you want (which is what I think based on the need something like part of the question) a hexadecimal representation of the characters in dataToSend
:如果你想要(这是我认为基于需要的东西像问题的一部分)在
dataToSend
中的字符的十六进制表示,请使用 IO 操纵器std::hex :
std::ostringstream convertStream;
convertStream << std::hex << std::setfill('0');
for (int i = 0; i < sendLength; i++)
{
convertStream << std::setw(2) << static_cast<short>(dataToSend[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.