Transform char array into String

FloIancu picture FloIancu · Jun 18, 2013 · Viewed 199.2k times · Source

I have a function that returns a char array and I want that turned into a String so I can better process it (compare to other stored data). I am using this simple for that should work, but it doesn't for some reason (bufferPos is the length of the array, buffer is the array and item is an empty String):

for(int k=0; k<bufferPos; k++){
      item += buffer[k];
      }

The buffer has the right values and so does bufferPos, but when I try to convert, for example 544900010837154, it only holds 54. If I add Serial.prints to the for like this:

for(int k=0; k<bufferPos; k++){
                  Serial.print(buffer[k]);
                  Serial.print("\t");
                  Serial.println(item);
                  item += buffer[k];
                }

the output is this:

5   
4   5
4   54
9   54
0   54
0   54
0   54
1   54
0   54
8   54
3   54
7   54
1   54

What am I missing? It feels like such a simple task and I fail to see the solution...

Answer

user2019047 picture user2019047 · Jun 18, 2013

If you have the char array null terminated, you can assign the char array to the string:

char[] chArray = "some characters";
String String(chArray);

As for your loop code, it looks right, but I will try on my controller to see if I get the same problem.