Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform char array into String

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

like image 549
FloIancu Avatar asked Jun 18 '13 00:06

FloIancu


3 Answers

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.

like image 67
user2019047 Avatar answered Oct 28 '22 15:10

user2019047


Three years later, I ran into the same problem. Here's my solution, everybody feel free to cut-n-paste. The simplest things keep us up all night! Running on an ATMega, and Adafruit Feather M0:

void setup() {   // turn on Serial so we can see...   Serial.begin(9600);    // the culprit:   uint8_t my_str[6];    // an array big enough for a 5 character string    // give it something so we can see what it's doing   my_str[0] = 'H';   my_str[1] = 'e';   my_str[2] = 'l';   my_str[3] = 'l';   my_str[4] = 'o';   my_str[5] = 0;  // be sure to set the null terminator!!!    // can we see it?   Serial.println((char*)my_str);    // can we do logical operations with it as-is?   Serial.println((char*)my_str == 'Hello');    // okay, it can't; wrong data type (and no terminator!), so let's do this:   String str((char*)my_str);    // can we see it now?   Serial.println(str);    // make comparisons   Serial.println(str == 'Hello');    // one more time just because   Serial.println(str == "Hello");    // one last thing...!   Serial.println(sizeof(str)); }  void loop() {   // nothing } 

And we get:

Hello    // as expected 0        // no surprise; wrong data type and no terminator in comparison value Hello    // also, as expected 1        // YAY! 1        // YAY! 6        // as expected 

Hope this helps someone!

like image 21
Rich Martin Avatar answered Oct 28 '22 17:10

Rich Martin


Visit https://www.arduino.cc/en/Reference/StringConstructor to solve the problem easily.

This worked for me:

char yyy[6];

String xxx;

yyy[0]='h';

yyy[1]='e';

yyy[2]='l';

yyy[3]='l';

yyy[4]='o';

yyy[5]='\0';

xxx=String(yyy);
like image 26
Eswar Avatar answered Oct 28 '22 17:10

Eswar