Arduino Char to String

  1. Convert char to String Using the String() Function in Arduino
  2. Convert char to String Using the Serial.readString() Function in Arduino

This tutorial will discuss two methods to convert a char into a String. The first method is to use the String() function, and the second method is to use the Serial.readString() function.

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

To convert char to String we can use the String() function. This function takes a variable as an input and returns a String object.

void loop(){
    char myChar = 'char';
    String myString = String(myChar); 
}

In the above code, myChar is a variable of type char to store the given char and myString is a variable of type String to store the conversion result.

Convert char to String Using the Serial.readString() Function in Arduino

If you are reading data of type char from a serial port in Arduino and want to convert the data into String, you can do that using the Serial.readString() function. Using this function, you can convert the received data directly rather than converting it later with another function. The Serial.readString() function reads char data from the serial port and converts it into a String.

void loop(){
    String myString = Serial.readString();
}

In the above code, myString is a variable of type String to store the data from the serial port. Note that this function will be terminated if it times out. Check setTimeout() to set the time out of the serial.

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 Serial Read String