# byte buffer to String object

**URL:** <https://forum.arduino.cc/t/byte-buffer-to-string-object/679607>\
**Category:** Programming\
**Created:** [October 22, 2020, 7:28pm UTC](https://forum.arduino.cc/t/byte-buffer-to-string-object/679607 "2020-10-22T19:28:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwg605](https://avatars.discourse-cdn.com/v4/letter/g/ba9def/32.png) [@gwg605](https://forum.arduino.cc/u/gwg605)\
**Post date:** [October 22, 2020, 7:28pm UTC](https://forum.arduino.cc/t/byte-buffer-to-string-object/679607/1 "2020-10-22T19:28:45Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [October 22, 2020, 7:31pm UTC](https://forum.arduino.cc/t/byte-buffer-to-string-object/679607/2 "2020-10-22T19:31:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [October 22, 2020, 7:33pm UTC](https://forum.arduino.cc/t/byte-buffer-to-string-object/679607/3 "2020-10-22T19:33:17Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![gwg605](https://avatars.discourse-cdn.com/v4/letter/g/ba9def/32.png) [@gwg605](https://forum.arduino.cc/u/gwg605)\
**Post date:** [October 26, 2020, 10:24pm UTC](https://forum.arduino.cc/t/byte-buffer-to-string-object/679607/4 "2020-10-26T22:24:05Z")

</div>

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:

```cpp
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:

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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:48am UTC](https://forum.arduino.cc/t/byte-buffer-to-string-object/679607/5 "2021-05-06T00:48:53Z")

</div>


