About turing a uint8_t array into String

hi guys:
I am trying to turn a 8 size uint8_t array into String. I wonder if I can turn it in without using for-loop.

Do you want turn it into a String object or only an ordinary C string?

Common to both is, that you need to terminate the string by adding a '\0' char (is equal 0 byte) at the end.

If you’re happy with a C string, than that’s all you need, since C strings are nothing else than and “array” of bytes/chars terminated by '\0'.

If you want a String object you’d pass a const char* to the String constructor, e.g. like this String((const char*)myByteArray);.

2 Likes

i see. I also have another question. Is that possible for using Serial.print( ) to print the address of pointer or object?

Yes, it is with something like Serial.println((uint32_t)myPtr, HEX);.

2 Likes

This is helpful, but could you elaborate.
I want to change a Particle Variable string type each time a function gets called, not create a new instance.
The string would each time be generated from a changing char array.
Thanks.

To change the content of a Particle.variable("StringVar", charArray, STRING);, you’d just copy the new content of the string into the char array (e.g. via strcpy(), String.toCharArray(), …).

For a Particle.variable("StringVar", stringObject); you’d just alter the content of the underlying String object.

I get the first bit - copying the String to a char array. That has a nice method call to do it.

Going the other way, from my existing char array, how exactly do I change the underlying string object.
I’ve tried looping with a for loop and concatenating a new the string, but that didn’t seem to work.
I can make my char array null terminated with a zero, but I’m still fighting this.
Thanks

Edit: This gave an error: invalid user-deined conversion, where aString is my Particle.variable and cmdbuf is my unsigned char buffer.

					  strcpy(aString , (const char*)cmdbuf0[0]);

Edit: I have now got something working… Thanks