how to convert char * to string

Hi

Does anyone know how I could convert char *variable for exemple
char *message="Hello";
to a string, to have
string messconv="Hello"; after doing a conversion ?

Thank you

1 Like

Have you try this:

char buf[] = "hello";

String mystring(buf);      //convert char array to string here.
1 Like

arduino_new:
Have you try this:

char buf[] = "hello";

String mystring(buf);      //convert char array to string here.

No, I didn't understand how to implement your exemple. I also tried and it didn't work. Also, as we can see in my first post I have char *message. There is a star. In your exemple, there is no star after the char.

1 Like

I found this:

char *message = "Hello";

String myString = String(message);

from this: byte array to String - Programming Questions - Arduino Forum

Also:

char *message = "Hello";

and

char message[] = "Hello";

are essentially the same. The only different is where the "Hello" resides in memory.

Perhaps equally important is why you would want to do that? A string built from a char array is going to use fewer resources than the String class.

arduino_new:
Also:

char *message = "Hello";

and

char message[] = "Hello";

are essentially the same. The only different is where the "Hello" resides in memory.

no, that is not correct.

this defines a pointer to a string literal

char *message = "Hello";

the string in this case is immutable

this defines an array of characters initialized with "Hello":

char message[] = "Hello";

this string may be modified.

OP just needs to read about the String class

1 Like