Convert character array to string in Arduino

ArduinoArduino BoardsArduino IDEArduino Programming Language

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

raja
Published on 24-Mar-2021 05:19:15
Advertisements