Transform char array into String

2020-02-25 07:29发布

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

5条回答
The star\"
2楼-- · 2020-02-25 07:50

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);
查看更多
Anthone
3楼-- · 2020-02-25 07:55

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!

查看更多
做个烂人
4楼-- · 2020-02-25 08:04

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.

查看更多
迷人小祖宗
5楼-- · 2020-02-25 08:08

I have search it again and search this question in baidu. Then I find 2 ways:

1,

char ch[]={'a','b','c','d','e','f','g','\0'};
string s=ch;
cout<<s;

Be aware to that '\0' is necessary for char array ch.

2,

#include<iostream>
#include<string>
#include<strstream>
using namespace std;

int main()
{
	char ch[]={'a','b','g','e','d','\0'};
	strstream s;
	s<<ch;
	string str1;
	s>>str1;
	cout<<str1<<endl;
	return 0;
}

In this way, you also need to add the '\0' at the end of char array.

Also, strstream.h file will be abandoned and be replaced by stringstream

查看更多
ら.Afraid
6楼-- · 2020-02-25 08:09

May you should try creating a temp string object and then add to existing item string. Something like this.

for(int k=0; k<bufferPos; k++){
      item += String(buffer[k]);
      }
查看更多
登录 后发表回答