将char数组转换为String [英] Transform char array into String

查看:1206
本文介绍了将char数组转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回char数组的函数,我希望将其转换为String,以便更好地处理它(与其他存储的数据进行比较).我使用这个简单的方法应该可以,但是由于某些原因(bufferPos是数组的长度,buffer是数组,item是空字符串),它不能:

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];
      }

buffer具有正确的值,bufferPos也具有正确的值,但是当我尝试转换时,例如544900010837154,它仅容纳54.

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];
                }

输出是这样的:

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

推荐答案

如果终止了char数组null,则可以将char数组分配给字符串:

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.

这篇关于将char数组转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆