简体   繁体   English

在 C++ 中将 uint8_t 数组转换为字符串

[英]convert uint8_t array to string in c++

This can be marked solved.这可以标记为已解决。 The problem was the print macro.问题是打印宏。 ESP_LOGx can't put out c++ Strings. ESP_LOGx 无法输出 c++ 字符串。

I'm trying to convert an uin8_t array to a string in c++.我正在尝试将 uin8_t 数组转换为 C++ 中的字符串。 The array is defined in a header file like this:该数组在头文件中定义,如下所示:

uint8_t mypayload[1112];

Printing the array itself works, so I'm sure it's not empty.打印数组本身可以工作,所以我确定它不是空的。

now I'm trying to convert it to a string:现在我正在尝试将其转换为字符串:

string qrData; 
std::string qrData(reinterpret_cast<char const*>(mypayload), sizeof mypayload);

I also tried: qrData = (char*)mypayload;我也试过: qrData = (char*)mypayload;

printing the string results in 5 random chars.打印字符串会产生 5 个随机字符。

Does anybody have hint where I made a mistake?有没有人暗示我在哪里犯了错误?

The only correct comment so far is from Some programmer dude.到目前为止,唯一正确的评论来自一些程序员老兄。 So all credits go to him.所以所有的功劳都归他所有。

The comment from Ian4264 is flat wrong. Ian4264 的评论完全错误。 Of course you can do a reinterpret_cast.当然你可以做一个 reinterpret_cast。

Please read here about the constructors of a std::string .在此处阅读有关std::string的构造函数的信息。 You are using constructor number 4. The description is:您正在使用 4 号构造函数。描述是:

4) Constructs the string with the first count characters of character string pointed to by s. 4) 用 s 所指向的字符串的前 count 个字符构造字符串。 s can contain null characters. s 可以包含空字符。 The length of the string is count.字符串的长度是计数。 The behavior is undefined if [s, s + count) is not a valid range.如果 [s, s + count) 不是有效范围,则行为未定义。

So, even if the string contains 0 characters, the C-Style string-"terminator", all bytes of the uint8_t arrays will be copied.因此,即使字符串包含 0 个字符,即 C 样式字符串 -“终止符”,uint8_t 数组的所有字节也会被复制。 And if you print the string, then it will print ALL characters, even the none printable characters after the '\0'.如果你打印字符串,那么它将打印所有字符,甚至是'\0'之后的不可打印字符。

That maybe your "random" characters.那可能是您的“随机”字符。 Because the string after your "terminator" does most probably contain uninitialized values.因为“终结符”之后的字符串很可能包含未初始化的值。

You should consider to use the constructor number 5您应该考虑使用 5 号构造函数

5) Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by s. 5) 构造字符串,其内容由 s 指向的以空字符结尾的字符串的副本初始化。 The length of the string is determined by the first null character.字符串的长度由第一个空字符确定。 The behavior is undefined if [s, s + Traits::length(s)) is not a valid range.如果 [s, s + Traits::length(s)) 不是有效范围,则行为未定义。

And if you need to add bytes, also possible.如果需要添加字节,也是可以的。 The std::string can grow dynamically. std::string可以动态增长。

BTW: you do define your "std::string qrData" double, which will not compile顺便说一句:你确实定义了你的“std::string qrData”double,它不会编译

Since you know the size of your data in another variable, why are you using sizeof ?既然您知道另一个变量中数据的大小,为什么要使用sizeof It will give you the size of the array , not the size of your data .它会给你数组的大小,而不是你的数据的大小。

This should give you the right result, assuming no other errors in your code假设您的代码中没有其他错误,这应该会给您正确的结果

std::string qrData(reinterpret_cast<char const*>(mypayload), data->payload_len);

Incidentally in the code you quoted why is qrData declared twice?顺便说一句,在您引用的代码中,为什么qrData声明了两次? That seems a bit suspicious.这似乎有点可疑。

qrData = (const char*)mypayload;

string 只接受 const char*。

String s = String((char *)data, len); //esp32

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

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