Arduino Convert String to Char

  1. Convert String to char Using the toCharArray() Function in Arduino
  2. Convert Data to char Using the toCharArray() Function and Append Operator in Arduino

This tutorial will discuss a method to convert a string into char - the toCharArray() function. It will also introduce how to convert other data types into char using the toCharArray() function and append operator.

Convert String to char Using the toCharArray() Function in Arduino

This method copies the string’s characters to the supplied buffer. It requires two inputs, one is a buffer to copy the characters into, and the other is the buffer size.

void loop(){
    String stringOne = "A string";
    char Buf[50];
    stringOne.toCharArray(Buf, 50)
}

In the above code, stringOne is the String object where the string is stored. Buf is the char array where the result is going to be saved. In this example, we use a buffer length of 50, but you can change that according to the given string. Check the link for more details.

Convert Data to char Using the toCharArray() Function and Append Operator in Arduino

If you want to convert any other data type instead of String, you can use it. First of all, you need to convert the other data type into String using the append operator; then, you can use the above method to convert this String into char.

void loop(){
    String stringOne = "A long integer: ";
    stringOne += 1234;
    char charBuf[50];
    stringOne.toCharArray(charBuf, 50)
}

In the above code, we have an integer - 1234 and add it to an existing String using the append operator to convert them into a char array. Check this link for more information.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Arduino String

  • Arduino Dtostrf Function
  • Arduino Char to String
  • Related Article - Arduino String

  • Convert Integer to String in Arduino
  • Arduino strtok