byte array to String

Hi,

how can I convert a byte array to Sting?

Either I am to stupid to search nobody required it so far. Most likely the first option :slight_smile:

Thanks
Robert

But you definitely don't want to use the String class. Search the forum for it and you'll find enough reasons not to use it.

String myString = String(myByteArray);

how can I convert a byte array to Sting?

I don't believe I've ever heard of a Sting class. Are you raising bees? Or scorpions?

4 Likes

PaulS:

how can I convert a byte array to Sting?

I don't believe I've ever heard of a Sting class. Are you raising bees? Or scorpions?

Neither - he's an FBI agent looking to entrap a rival CIA agent :wink:

2 Likes

majenko:

String myString = String(myByteArray);

String() - Arduino Reference

Hi,

thanks for the answer. That's what I tried first. It works fine with a char array but not with an array of bytes.
Maybe my mistake is somewhere else.

Here my examples:

fails with "call of overloaded 'String(byte [5])' is ambiguous" :

void setup(void){}
void loop(void)
{
byte byteArray[5];
strcpy((char *)byteArray,"0123");
String myString = String(byteArray);
}

compiles fine:

void setup(void){}
void loop(void)
{
char byteArray[5];
strcpy((char *)byteArray,"0123");
String myString = String(byteArray);
}

Thanks
Robert

Ah, byte is a typedef for "unsigned char" - there is no unsigned char prototype for the String constructor.

Cast it to (char *) and it should be fine.

void setup(void)
{
}

void loop(void)
{
  byte byteArray[5];
  strcpy((char *)byteArray,"0123");
  String myString = String((char *)byteArray);
}

majenko:
Ah, byte is a typedef for "unsigned char" - there is no unsigned char prototype for the String constructor.
Cast it to (char *) and it should be fine.

Perfect!
Thanks for the great help.

Robert

robvoi:
Hi,

how can I convert a byte array to Sting?

Either I am to stupid to search nobody required it so far. Most likely the first option :slight_smile:

Thanks
Robert

hi all,

this worked for me

String myString = String((char*)myByteArray);
1 Like

this worked for me

That calls two constructors, the equal operator, and the destructor.

String myString = (char*)myByteArray;

Does the same thing with only one call to the constructor.

Hi, im not making an answer, but i have a problem, i dont't really know what to do.

im trying to use this:

String myString = String(myByteArray);

but doesn't work and returns me this:

no matching function for call to 'String(void (&)(byte*, byte))'

i'm trying to do it with Byte Buffer, because i need to convert to String theme i would send to dataBase.

i would appreciate so much if someone helps me

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.