在 Arduino 上将 int 或 String 转换为 char 数组 [英] Converting an int or String to a char array on Arduino

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

问题描述

我从我的 Arduino 上的一个模拟引脚获得一个 int 值.如何将其连接到 String,然后将 String 转换为 char[]?

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[]?

有人建议我尝试 char msg[] = myString.getChars();,但我收到一条消息,指出 getChars 不存在.

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

推荐答案

  1. 要转换和附加整数,请使用operator +=(或成员函数concat):

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

  • 要获取类型为 char[] 的字符串,请使用 toCharArray():

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

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

  • 在示例中,只有 49 个字符的空间(假设它以 null 结尾).您可能希望动态调整大小.

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

    ###开销

    引入String<的成本/code>(如果未在草图中的任何地方使用,则不包括在内),大约为 1212 字节的程序存储器(闪存)和 48 字节的 RAM.

    The cost of bringing in String (it is not included if not used anywhere in the sketch), is approximately 1212 bytes of program memory (flash) and 48 bytes RAM.

    这是使用 Arduino IDE 版本 1.8.10 (2019-09-13) 测量的 Arduino Leonardo 草图.

    This was measured using Arduino IDE version 1.8.10 (2019-09-13) for an Arduino Leonardo sketch.

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

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