1

I'm trying to convert a byte array into a string using the following code.

String b = String((char*)buffer2);

but when I output the string I get a very strange result that contains white spaces and special characters that normally shouldn't be in the string.

buffer2 is of the type byte and its length is 18. this is how its declared:

byte buffer2[18];

When I use the following code to print the byte array I get the results I expect.

for (uint8_t i = 0; i < 16; i++) {
    Serial.write(buffer2[i] );
}

I'm wondering how I can convert a byte array to a string the proper way.

Alexander Hörl
  • 578
  • 4
  • 11
  • 24
  • And what are the contents of `buffer2`? – gre_gor Apr 04 '18 at 17:41
  • 1
    And what is `buffer2`? How was it declared, defined and initialized? Does it, most importantly, contain a *string terminator*? – Some programmer dude Apr 04 '18 at 17:41
  • @gre_gor Oh sorry its of the type byte and length is 18. byte buffer2[18]; – Alexander Hörl Apr 04 '18 at 18:01
  • @Someprogrammerdude Oh sorry its of the type byte and length is 18. byte buffer2[18]; I guess it doesn't contain a string terminator its only a name. – Alexander Hörl Apr 04 '18 at 18:02
  • I am asking about the contents. Crate a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – gre_gor Apr 04 '18 at 18:03
  • So if it's a "name" it could be *shorter* than the 16 bytes you use from the array? If you have a C-style string, then add a terminator and check for that (and the length). – Some programmer dude Apr 04 '18 at 18:05
  • @Someprogrammerdude I'm new to c++ how can I add a terminator and check for that? also I don't know the content of the variable and its length in the final version so the length should be determined dynamically. – Alexander Hörl Apr 04 '18 at 18:12
  • Then I recommend [getting a good book or two (or three or more)](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). The terminator is the character `'\0'`. You add it after the last character you add to the buffer. And you check for it by normal comparison. – Some programmer dude Apr 04 '18 at 18:15
  • There is another solution to your problem as well, and that is to keep track of the number of characters in the array (in a variable), and use that as the upper limit of the loops where you iterate over the characters. – Some programmer dude Apr 04 '18 at 18:17
  • @Someprogrammerdude isn't there a way to convert the buffer to a string without having to add \0 int the first place? – Alexander Hörl Apr 04 '18 at 18:18
  • No. A `char` string in C++ is actually named ***null-terminated** byte string*. It is the terminator that turns an array of characters into a string. – Some programmer dude Apr 04 '18 at 18:20
  • @Someprogrammerdude but when I don't convert it to a string but a char array would this solve my problem? – Alexander Hörl Apr 04 '18 at 18:23
  • As I said in one of my later comments, you could skip the terminator *if you keep track of the current size*. It's not a *string* though, just an array of `char` elements. – Some programmer dude Apr 04 '18 at 18:25
  • @Someprogrammerdude is there a way to get how much chars are in the byte array? The I could use this number to convert to char array. – Alexander Hörl Apr 04 '18 at 18:29
  • That's why I keep telling you, *no* there isn't. Either you have a terminator, *or* you have a variable which will tell you. How do you fill the array? Where do the characters come from? You must initialize the contents *somewhere* in your code. There you can do either solution: Add terminator each and every time you add a new character, *or* save the position of the last character plus one in a variable as the length. – Some programmer dude Apr 04 '18 at 18:33
  • @Someprogrammerdude The contents of the variable are gathered by a function that reads to content of a rfid card. – Alexander Hörl Apr 04 '18 at 18:35

0 Answers0