[SOLVED] Uint8_t Buffer To String - Arduino Forum

[SOLVED] uint8_t buffer to String Projects Programming August 16, 2015, 6:25pm 1

May Peace and Blessings be upon you!

I've been looking around for some reference to convert my whole buffer of uint8_t to use as a String. The reason was the String class offers us much easy to use and diverse methods or functions to process the data. This thing usually comes across to us when we are using some library and the library processes and gives you some data into a char* or uint8_t type of buffer. After getting the data in the buffer new comers usually don't know how to handle and process it. I had a similar scenario, but when I try to use google and this forum I didn't find much reliable solution to do it, and then I figured out some other way to achieve it using type casting.

Here is my solution:

if you have some kind of data buffer of uint8_t e.g:

uint8_t buff[700] = {0};

and you want to convert all the buffer to the String. Then just call this code below and you will have your buffer as a String object.

String str = (char*)buff;

and now you can use this String object to use any of its methods e.g:

char L = str.length();

OR

String new_str = str.substring(8, 15);

easy han?

Warning: Strings takes some more memory on your Arduino than uint8_t, So, be sure that what you are going to do otherwise your Arduino might keep on resetting again and again if the memory bloats.

I hope it will help you.

Regards, Dexterous.

2 Likes ESP32 UDP read data August 17, 2015, 10:25am 2

I hope it will help you.

Learning to not use Strings would be even more helpful.

1 Like November 26, 2015, 4:55pm 3

Hi! :slight_smile:

Learning to not use Strings would be even more helpful.

Learning to really help others, would be even more useful to everyone. Bring up some richer arguments, with sources, recommendations or whatever supports your comments around forums.

Everyone at programming world knows that handling integers is better, the lower memory elements the better, maybe, but just maybe, that's why...

Warning: Strings takes some more memory on your Arduino than uint8_t, So, be sure that what you are going to do otherwise your Arduino might keep on resetting again and again if the memory bloats.

So, for the record. This recipe posted by @Dexterous comes very in handy when working with HTTP responses when working with C at another microcontroller solution, but well, I suppose that even with that case of use, the String class doesn't seem to have any sense of existing... :stuck_out_tongue:

Dexterous, thanks for this straightforward recipe and for your blessings!

Good vibes to all!

Jon.

May 15, 2016, 10:28am 4

Most useful post I found in days of trawling the net! Thanks for this, I can now convert uin8_t to strings which has been causing me trouble for days :slight_smile:

May 15, 2016, 3:01pm 5

Hmm... self-SOLVED pseudo-questions and praise from newbies? Be warned. Don't use String™. Using the String class will cause you trouble forever.

Maybe the OP should read this recent discussion, this summary of the issues from all over the innertubes, and this cautionary tale. If you still wonder if String is useful, read this.

OP, if you want "to really help others", ask a useful question or provide a useful answer. Don't claim to solve the wrong problem for your 3rd post. If you have a 700-byte array and you convert it to a String, that allocates another 700 bytes. Using 1400 bytes for an HTTP response to extract one uint8_t is no solution -- it's unbelievably wasteful. The HTTP response should have been parsed as it was received.

And if you don't understand how to do that, ASK a question that may help others.

1 Like May 15, 2016, 7:57pm 6

Good to know. It's fair to say that after posting the praise above I fell at the next hurdle in my code. Learning this stuff is difficult and there seems to be multiple ways to do each thing. I was trying to convert to a string as I needed to reference individual characters in the uinit8_t. It seemed that charAt could only parse strings from the references I read. I later tried it on the uinit8_t and it worked! The string reference pages could use a big "do not use" header, as when beginig to learn I'm sure most people will follow these reference instructions before trawling forums and asking questions.

May 15, 2016, 8:10pm 7

The string reference pages could use a big "do not use" header

Now THAT's a useful post!

Since it wasn't clear, I edited my post to address it to the OP. But thanks for adding your experience. That's so much more convincing than "don't do that."

Cheers, /dev

Topic Replies Views Activity
How to convert uint8_t buffer to a propper String Programming 9 3946 May 6, 2021
ASCII Char array in uint8 wandeln --> Hex to uint Deutsch 25 190 November 7, 2025
Conversion from 'uint8_t' {aka 'unsigned char'} to 'String' is ambiguous Programming 4 431 June 16, 2024
How to convert a substring to uint8_t or int8_t Programming 5 661 April 9, 2024
Uint8_t to string Programming 37 17413 February 3, 2023
Unfortunately, your browser is unsupported. Please switch to a supported browser to view rich content, log in and reply.

Tag » Arduino Uint8_t Buffer To String