转换一个int或字符串在Arduino的字符数组 [英] Converting an int or String to a char array on Arduino

查看:450
本文介绍了转换一个int或字符串在Arduino的字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的Arduino模拟引脚的一个int值。我如何串连给字符串然后转换字符串的char []

有人建议我尝试味精的char [] = myString.getChars(); ,但我接收则GetChars 不存在。


解决方案

  1. 要转换并追加一个整数,使用的 运算符+ = 的(或成员函数 CONCAT

     字符串stringOne =一个长整型;
    stringOne + = 123456789;


  2. 要得到字符串类型的char [] ,使用 toCharArray()

     字符charBuf [50];
    stringOne.toCharArray(charBuf,50)


在该示例中,仅存在49个字符(presuming它是由空终止)的空间。你可能想使大小动态。

I am getting an int value from one of the analog pins on my Arduino. How do I concatenate this to a String and then convert the String to a char[]?

It was suggested that I try char msg[] = myString.getChars();, but I am receiving a message that getChars does not exist.

解决方案

  1. To convert and append an integer, use operator += (or member function concat):

    String stringOne = "A long integer: ";
    stringOne += 123456789;
    

  2. To get the string as type char[], use toCharArray():

    char charBuf[50];
    stringOne.toCharArray(charBuf, 50) 
    

In the example, there is only space for 49 characters (presuming it is terminated by null). You may want to make the size dynamic.

这篇关于转换一个int或字符串在Arduino的字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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