Array of bytes to string

hello to all .
I have some problems in coding .
How can I convert array of bytes to string.

byte a[]="Hello";

I want to save "Hello" in a string variable.

'a' is a string variable.
Problem solved.

No I mean wanna save 'a' variable in string variable.???

You are going to have to explain better. That's like asking how to turn 42 into a number.

Sorry for my poor English. :frowning:

leoncorleone:
No I mean wanna save 'a' variable in string variable.???

sp. "want to"

strcpy (your_string_variable, a);

I've tried your code,but I've got this error.
cannot convert 'String' to 'char*' for argument '1' to 'char* strcpy(char*, const char*)'

test.ino (122 Bytes)

Your program

byte a[]="Hello";

void setup() {
Serial.begin(9600);
}
void loop() {
  String b;
  strcpy(b,a);
  Serial.println(a);
}

A String is not a string and a is not a string in any case.

In any case, even if you used a C style string (lowercase c) as the destination, all your code would do is to print the original string a, not the one copied to.

A modified version of your program

char a[] = "Hello";

void setup()
{
  Serial.begin(115200);
  char b[6];
  strcpy(b,a);
  Serial.print("a ");
  Serial.println(a);
  Serial.print("b ");
  Serial.println(b);
}
void loop()
{
}

Can you please explain in more detail what you want to do and why and we can make suggestions as to how to do it.

Actually I have a variable of bytes array the same as below:

Bytes a[]=”Hello my dear , your password is : 12587”;

I want to separate some of words of it. And I don’t know how to do it with array of bytes.
I want to convert array of bytes to string variable first , and then use functions from String class to separate it .Please help me.

If you have an array of "bytes" (aka uint8_t) a simple cast will turn it into an array of "char"

Can u give me an example?thanks

byte a[]="Hello";
char b[6];
strcpy (b, (char*) a);

That is it,Thanks.:wink:

Another question I have, How can I separate some words of an array of bytes.
for example I have byte a[]="Hello my dear,your pass is :123456789"; and I want to put '123456789' to another variable.

strtok

I found this reference:
http://www.cplusplus.com/reference/cstring/
but it might be a little difficult for beginners to use.
You might want to take a look at it, though.

Does anyone here know of a good C string reference for beginners?
It would especially help the OP if it would explain how to use strtok().

Clearly, strtok() is the best way to solve the problem, but it is a little difficult to understand. This is simpler, but assumes a colon always precedes the PW:

byte a[] = "Hello my dear,your pass is :123456789";

void setup() {

  char *ptr;
  char pass[12];

  Serial.begin(9600);

  ptr = strchr((char *)a, ':');    // Look for the colon in the string...
  if (ptr != '\0') {               // Did we find it?
    ptr++;                         // Yep...so point one character past the colon...
    strcpy(pass, ptr); 
    Serial.println(temp);
  }
}

void loop() {
}

strtok is actually quite easy to understand, and use. On the first call, you pass a pointer to the string to be tokenized, and a pointer to a string containing the delimiter characters. It will return a pointer to the first token in the string. On subsequent calls, pass a NULL instead of the pointer to the string to be tokenized, and each call will continue from where it left off in the previous call. When no more tokens are left, it will return NULL.

char str[] = "Dude,  tokenize this!";
char delim[] = " ,!";

char *first = strtok(str, delim);  // sets first = "Dude"
char *second = strtok(NULL, delim); // sets second = "tokenize"
char *third = strtok(NULL, delim); // sets third = "this"
char *fourth = strtok(NULL, delim); // sets fourth = NULL

That's really all there is to it. It doesn't get much simpler...

Regards,
Ray L.

strtok is actually quite easy to understand, and use.

True...if you understand how pointers work. Reading the comments by the OP suggests he is not comfortable with pointers yet. I was simply trying to offer an alternative that might be easier for the OP to understand. It should also be mentioned that strtok() destroys the original string passed to it.