. Advertisement .
..3..
. Advertisement .
..4..
This tutorial will introduce you to the solution for Arduino convert string to char. It doesn’t involve standard C++ libraries but a custom method packed in these boards.
Arduino Convert String To Char
You can use the toCharArray()
method of a string instance to convert it to a char array. The syntax of this method is as follows:
myString.toCharArray(buf, len)
Parameters:
- myString: the string you want to convert to separate characters.
- buf: the buffer the method needs to copy those characters to. It needs to be an array of char.
- len: an integer that indicates the length of the buffer.
The toCharArray()
method neither modifies the myString parameter nor returns any value. You will find the resulting characters in the buffer parameter.
The following example shows how you can make use of this method to convert any string to an array of char:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
String myStr = "Hello! Welcome to ITTutoria.net!";
char buf[33];
myStr.toCharArray(buf, 33);
Serial.println(buf);
}
Output:
Hello! Welcome to ITTutoria.net!
This program creates a string first, which contains our welcome message. Before invoking the toCharArray()
method, we create an array of char, whose size equals the length of the string above.
At this point, our job is simple. We just need to call the toCharArray()
method of the original string and provide the buffer and its size as arguments.
The buf array now should store all the characters of the string. The println()
function can be used to print it. If you want to learn about printing char arrays in Arduino, have a look at this guide.
Remember that you must provide the correct size of the char array. It should be the number of characters in the string plus the terminating character – null.
If the buf parameter has a length smaller than the number of characters of your string, the toCharArray()
method will not be able to convert every character and put it into the resulting array.
Many newcomers forget to take the null character into account. This is a common mistake when they aren’t familiar with arrays of characters in low-level languages like C/C++. For instance, the toCharArray()
method will leave out the final character if you forget the existence of the null character:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
String myStr = "Hello! Welcome to ITTutoria.net!";
char buf[32];
myStr.toCharArray(buf, 32);
Serial.println(buf);
}
Output:
Hello! Welcome to ITTutoria.net
Likewise, Arduino will only convert and display a small portion of the string when you set the size of the buffer array too small.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
String myStr = "Hello! Welcome to ITTutoria.net!";
char buf[6];
myStr.toCharArray(buf, 6);
Serial.println(buf);
}
Output:
Hello
In this example, the size of 6 is, of course, far from the correct length of our string. The compiler will compile your code, and Arduino will still run it without throwing any error. But as you can see, it will only display the first five characters of the string. This means the toCharArray()
has converted only them and the null character.
On the other hand, you won’t encounter any error when the size of the buf array is bigger than the string’s length. You can take advantage of this and set an arbitrary size for the buffer array to avoid the task of counting the number of characters in the string.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
String myStr = "The quick brown fox jumps over the lazy dog.";
char buf[100];
myStr.toCharArray(buf, 100);
Serial.println(buf);
}
Output:
The quick brown fox jumps over the lazy dog.
Be cautious when using this trick, and don’t make a buffer too big. The larger the array, the more memory it consumes. Unlike your computer, Arduino boards have more limited memory. Optimize your program and save memory usage when possible.
Keep in mind that you can also use the append operator (+=)
when you want to add another string to the end of your original string and convert all of them to characters:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
String myStr = "The rank of our site is ";
myStr += 1;
char buf[30];
myStr.toCharArray(buf, 30);
Serial.println(buf);
}
Output:
The rank of our site is 1
Summary
You can use the toCharArray()
method when it comes to Arduino convert string to char. Make sure to give the array the right size as it determines how many characters the method will convert.
Leave a comment