简体   繁体   English

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

[英]Converting an int or String to a char array on Arduino

I am getting an int value from one of the analog pins on my Arduino.我从我的 Arduino 上的一个模拟引脚获得一个 int 值。 How do I concatenate this to a String and then convert the String to a char[] ?如何将其连接到String然后将String转换为char[]

It was suggested that I try char msg[] = myString.getChars();有人建议我尝试char msg[] = myString.getChars(); , but I am receiving a message that getChars does not exist. ,但我收到一条消息称getChars不存在。

  1. To convert and append an integer, use operator += (or member function concat ):要转换和附加整数,请使用运算符 += (或成员函数concat ):

     String stringOne = "A long integer: "; stringOne += 123456789;
  2. To get the string as type char[] , use toCharArray() :要获取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).在示例中,只有 49 个字符的空间(假设它以 null 结尾)。 You may want to make the size dynamic.您可能希望使尺寸动态化。

Overhead高架

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.引入String的成本(如果不在草图中的任何地方使用,则不包括在内),大约是 1212 字节的程序存储器(闪存)和 48 字节的 RAM。

This was measured using Arduino IDE version 1.8.10 (2019-09-13) for an Arduino Leonardo sketch.这是使用 Arduino IDE 版本 1.8.10 (2019-09-13) 对Arduino Leonardo草图进行测量的。

Just as a reference, below is an example of how to convert between String and 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 somehow it's the easiest way.是的,这对于像类型转换这样简单的事情来说是非常迟钝的,但不知何故这是最简单的方法。

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 发布字符串变量时,这将非常有用。

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

With all the answers here, I'm surprised no one has brought up using itoa already built in.有了这里的所有答案,我很惊讶没有人提出使用已经内置的 itoa。

It inserts the string representation of the integer into the given pointer.它将整数的字符串表示形式插入到给定的指针中。

int a = 4625;
char cStr[5];       // number of digits + 1 for null terminator
itoa(a, cStr, 10);  // int value, pointer to string, base number

Or if you're unsure of the length of the string:或者,如果您不确定字符串的长度:

int b = 80085;
int len = String(b).length();
char cStr[len + 1];  // String.length() does not include the null terminator
itoa(b, cStr, 10);   // or you could use String(b).toCharArray(cStr, len);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM