在 Arduino 上将 int 或 String 转换为 char 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7383606/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:12:53  来源:igfitidea点击:

Converting an int or String to a char array on Arduino

stringchararduino

提问by Chris

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

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

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

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

回答by Peter Mortensen

  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)
    
  1. 要转换和附加整数,请使用运算符 +=(或成员函数concat):

    String stringOne = "A long integer: ";
    stringOne += 123456789;
    
  2. 要将字符串作为 type char[],请使用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.

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

Overhead

高架

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

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

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

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

回答by Alex King

Just as a reference, here is an example of how to convert between Stringand char[]with a dynamic length -

作为参考,这是一个如何在动态长度Stringchar[]动态长度之间进行转换的示例-

// Define 
String str = "This is my string"; 

// Length (with one extra character for the null terminator)
int str_len = str.length() + 1; 

// Prepare the character array (the buffer) 
char char_array[str_len];

// Copy it over 
str.toCharArray(char_array, str_len);

Yes, this is painfully obtuse for something as simple as a type conversion, but sadly it's the easiest way.

是的,这对于像类型转换这样简单的事情来说是令人痛苦的迟钝,但遗憾的是,这是最简单的方法。

回答by Lê V? Linh

You can convert it to char* if you don't need a modifiable string by using:

如果您不需要可修改的字符串,可以使用以下方法将其转换为 char*:

(char*) yourString.c_str();

This would be very useful when you want to publish a String variable via MQTT in arduino.

当您想在 arduino 中通过 MQTT 发布字符串变量时,这将非常有用。

回答by user6776703

None of that stuff worked. Here's a much simpler way .. the label str is the pointer to what IS an array...

这些东西都没有奏效。这是一个更简单的方法......标签 str 是指向什么是数组的指针......

String str = String(yourNumber, DEC); // Obviously .. get your int or byte into the string

str = str + '\r' + '\n'; // Add the required carriage return, optional line feed

byte str_len = str.length();

// Get the length of the whole lot .. C will kindly
// place a null at the end of the string which makes
// it by default an array[].
// The [0] element is the highest digit... so we
// have a separate place counter for the array...

byte arrayPointer = 0;

while (str_len)
{
    // I was outputting the digits to the TX buffer

    if ((UCSR0A & (1<<UDRE0))) // Is the TX buffer empty?
    {
        UDR0 = str[arrayPointer];
        --str_len;
        ++arrayPointer;
    }
}