byte buffer to String object

Hi All,

Sorry for stupid question, but the search on fourm and google don't help me.

My question related to generic pattern: get string from a buffer.

I have bytes/ascii chars buffer. This buffer does NOT have string terminator.
I want to convert this whole buffer to String object. This is minimum. Maximum: specify offset and size string in the buffer.
Is there any method of String object or method that can do it?

NOTE: The appending byte by byte to string makes me (no proper words to express this).

Why do you need to convert the byte array to a String ?
How did the bytes get in the array in the first place ?

gwg605:
Hi All,

Sorry for stupid question, but the search on fourm and google don't help me.

My question related to generic pattern: get string from a buffer.

I have bytes/ascii chars buffer. This buffer does NOT have string terminator.
I want to convert this whole buffer to String object. This is minimum. Maximum: specify offset and size string in the buffer.
Is there any method of String object or method that can do it?

NOTE: The appending byte by byte to string makes me (no proper words to express this).

If you don't have a string terminator then you must know the size of the data in the buffer. If you have room in the buffer (or can increase it's size by one) you can just put the string terminator at the end of the data, then you have a C-string and can go from there.

I receive a network packet. The packet has binary format and the string is represented as sequence of bytes where first byte is size of the string and no terminator. ie packet contains data before and after string, multiple strings can be placed together or separated by another data.
Now I do as follow:

String cmd;
cmd.reserve( sreq->length );
for( uint8_t i = 0; i < sreq->length; i++ ) {
	cmd += sreq->cmd[ i ];
}

But something like this would be much better:

String cmd( sreq->cmd, sreq->length );