Arduino

How to Convert String to Characters Array in Arduino using toCharArray Function

The String data type is a popular choice for storing character data in the Arduino programming environment. Sometimes String objects needed to be converted into a char array. This is where the toCharArray() function comes in handy. This article features the use of toCharArray() function in Arduino IDE.

toCharArray() Function in Arduino

The toCharArray() function is used to convert a String object to a char array in the Arduino programming environment.

Syntax

The toCharArray() function in Arduino has the following syntax:

stringObject.toCharArray(charArray, length);

Here:

  • stringObject: is the name of the String object that you want to convert to a char array.
  • charArray: is the name of the char array that will store the contents of the String object.
  • Length: is the length of the String object and a null terminator.

Parameter Values

This function takes two arguments:

1: The first argument is the name of the char array that will store the contents of the String object

2: The second argument is the length of the String object and also includes the null terminator. The null terminator is an extra character that indicates the end of the String data.

Here is an example of how to use the toCharArray() function in the Arduino program:

String str = "Hello";
char charArray[20];
str.toCharArray(charArray, str.length() + 1);

In this example, the contents of the String object str are copied into the charArray. The length of the String object is obtained using the length() function and is then used as the second argument in the toCharArray() function.

It is important to note that the char array defined size should be enough so that it can hold the contents of the String object, plus the null terminator. If the char array is too small, the toCharArray() function will only copy a portion of the String object and the rest of the data will be lost.

The toCharArray() is used during serial communication. For example, you may want to send a String object over the serial port and then receive it on another device as a char array. To do this, you can use the toCharArray() function to convert the String object to a char array, and then send the char array over the serial port.

Example Code

Here is an example program that demonstrates the use of the toCharArray() function in the Arduino environment:

#include <Arduino.h>
void setup() {
 Serial.begin(9600);
}
void loop() {
 String str = "Hello World";
 char charArray[20];
 str.toCharArray(charArray, str.length() + 1);
 for (int i = 0; i < str.length() + 1; i++)
 Serial.println(charArray[i]);
 delay(1000);
}

In this example, a String object named str is defined and is assigned the value “Hello World”. The contents of the String object are then copied into the charArray using the toCharArray() function. The length of the String object is obtained using the length() function and is then used as the second argument in the toCharArray() function.

Finally, the contents of the char array are printed to the serial port using the Serial.println() function. The delay() function is used to slow down the rate at which the data is sent over the serial port.

Conclusion

The toCharArray() function is a useful tool for converting a String object to a char array in the Arduino programming environment. By using this function, you can easily work with String data in a variety of different contexts, including serial communication and other data-processing tasks.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.