1

This can be marked solved. The problem was the print macro. ESP_LOGx can't put out c++ Strings.

I'm trying to convert an uin8_t array to a string in 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;

printing the string results in 5 random chars.

Does anybody have hint where I made a mistake?

6
  • Remember that char strings are really called null-terminated byte strings. If you have an array of bytes that you want to use as a string, then you need to make sure it's terminated by a "null" character ('\0' or just plain zero). Commented Mar 28, 2020 at 7:24
  • thanks for the reply! I already added a terminator at creation of the array via char terminator = '\0'; memcpy(&mypayload[data->payload_len], &terminator, 1); Commented Mar 28, 2020 at 7:33
  • You cannot just cast the array into a string. You need to do some logic to iterate over the array appending each number to a string with some sort of separator between each one. I would suggest using an ostringstream. See cplusplus.com/reference/sstream/ostringstream/str for an example Commented Mar 28, 2020 at 7:35
  • 1
    sizeof ? Why? You may want to reconsider that. Commented Mar 28, 2020 at 7:36
  • 1
    From the incomplete code you give, it seems impossible to pinpoint the exact problem with the code. It is possible that the error lays in order portions of your code. Please try running your code through a debugger. You should identify the problematic portion of the code and see if you can solve the error by searching on the Internet. If not, extract the problematic code and make a minimal reproducible example (which can be fed directly to a compiler and demonstrate the problem) to demonstrate it. Make sure to tackle one specific issue and post the minimal code required to reproduce the problem. Commented Mar 28, 2020 at 7:42

4 Answers 4

2

The only correct comment so far is from Some programmer dude. So all credits go to him.

The comment from Ian4264 is flat wrong. Of course you can do a reinterpret_cast.

Please read here about the constructors of a std::string. You are using constructor number 4. The description is:

4) Constructs the string with the first count characters of character string pointed to by s. s can contain null characters. The length of the string is count. The behavior is undefined if [s, s + count) is not a valid range.

So, even if the string contains 0 characters, the C-Style string-"terminator", all bytes of the uint8_t arrays will be copied. And if you print the string, then it will print ALL characters, even the none printable characters after the '\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) Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by 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.

And if you need to add bytes, also possible. The std::string can grow dynamically.

BTW: you do define your "std::string qrData" double, which will not compile

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply! To be fair I am fairly new to this and understand ~20% of the source you linked. I have the length of mypayload at hand as mypayload_len which is constructed via mypayload_len = data->payload_len +1and tried to passing it, which sadly leads to the same results. So I don't think this is not the problem.
0

Since you know the size of your data in another variable, why are you using 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? That seems a bit suspicious.

1 Comment

OK... My original code works.. The ESP print macro just seems to have a problem. Goddam hours...
0
qrData = (const char*)mypayload;

string is accept only const char*.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.