Convert character array to string in Arduino


In order to convert a character array to a string, the String() constructor can be used. An example is shown below −

Example

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
   Serial.println();
   char buf[10] = "Hello!";

   Serial.print("Char array: ");
   Serial.println(buf);
   String s = String(buf);
   Serial.print("String: ");
   Serial.println(s);
}
void loop() {
   // put your main code here, to run repeatedly:
}

The output of the Serial monitor is shown below −

Output

Updated on: 24-Mar-2021

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements