string 将字符数组转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17158890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transform char array into String
提问by FloIancu
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):
我有一个函数返回一个字符数组,我希望它变成一个字符串,这样我就可以更好地处理它(与其他存储的数据相比)。我正在使用这个简单的方法应该可以工作,但由于某种原因它不起作用(bufferPos
是数组的长度,buffer
是数组并且item
是空字符串):
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:
Thebuffer
具有正确的值,因此bufferPos
,但是当我尝试转换时,例如 544900010837154,它只包含 54。如果我将 Serial.prints 添加到 for 中,如下所示:
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...
我错过了什么?感觉就像一个如此简单的任务,我看不到解决方案......
回答by user2019047
If you have the char array null terminated, you can assign the char array to the string:
如果 char 数组 null 终止,则可以将 char 数组分配给字符串:
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.
至于你的循环代码,它看起来不错,但我会在我的控制器上试一试,看看我是否遇到同样的问题。
回答by Rich Martin
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:
三年后,我遇到了同样的问题。这是我的解决方案,每个人都可以随意剪切粘贴。最简单的事情让我们彻夜难眠!在 ATMega 和 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!
希望这可以帮助某人!
回答by Eswar
Visit https://www.arduino.cc/en/Reference/StringConstructorto solve the problem easily.
访问https://www.arduino.cc/en/Reference/StringConstructor轻松解决问题。
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]='for(int k=0; k<bufferPos; k++){
item += String(buffer[k]);
}
';
xxx=String(yyy);
回答by praks411
May you should try creating a temp string object and then add to existing item string. Something like this.
您应该尝试创建一个临时字符串对象,然后添加到现有的项目字符串中。像这样的东西。
char ch[]={'a','b','c','d','e','f','g','#include<iostream>
#include<string>
#include<strstream>
using namespace std;
int main()
{
char ch[]={'a','b','g','e','d','##代码##'};
strstream s;
s<<ch;
string str1;
s>>str1;
cout<<str1<<endl;
return 0;
}
'};
string s=ch;
cout<<s;
回答by sunshinestyle
I have search it again and search this question in baidu. Then I find 2 ways:
我又搜索了一遍,在百度里搜索了这个问题。然后我找到了两种方法:
1,
1、
Be aware to that '\0' is necessary for char array ch.
请注意,字符数组 ch 需要 '\0'。
2,
2、
In this way, you also need to add the '\0' at the end of char array.
这样,您还需要在char数组的末尾添加'\0'。
Also, strstream.h file will be abandoned and be replaced by stringstream
此外, strstream.h 文件将被放弃并被 stringstream 替换