# [SOLVED] String to Char

Hello my friends,

I need to convert this string: String get_result = "0x01,0x03,0x1B,0x59,0x00,0x01,0x52,0xFD";
to char message.

I need this result: message = { 0x01, 0x03, 0x1B, 0x59, 0x00, 0x01, 0x52, 0xFD}.
when i get message[1] = 0x01

How i do this?

Best regards,
Joao Daniel

Look into strTok()

“0x01,”

is 5 chars long 6 if there’s a null at the end while 0x01 is just 1 char!

Mark

Look into strTok()

Or strtok(), to be used only after you shitcan the String class, and define a string (a NULL terminated array of chars).

when i get message[1] = 0x01

message[ 1 ] is 0x03.

message[ 0 ] is 0x01.

Well it would help if we knew where he was getting that "S/string" from. He might not even need strTok() at all.

0X01 is 1byte, total of message is 9bytes.

i will try to use the strtok.

I need to receive a string like this String resposta = "0x01,0x03,0x1B,0x59,0x00,0x01,0x52,0xFD";

and i need to put byte by byte in a byte message[9].

To send to a serial port.

i try to use but only return 0x

Best Regards.

Try something like this (not tested; no error checking):

``````String resposta = "0x01,0x03,0x1B,0x59,0x00,0x01,0x52,0xFD";

int message[9] = {0};
int i = 0;  // string index;
int j = 0;  // message[] index;

while (i<resposta.length())
{
// assuming all substrings are 4 char long;
message[j] = resposta.substring(i, i+4).toInt();
i = i + 5;
j++;
}
``````

florinc:
Try something like this (not tested; no error checking):

``````String resposta = "0x01,0x03,0x1B,0x59,0x00,0x01,0x52,0xFD";
``````

int message[9] = {0};
int i = 0;  // string index;
int j = 0;  // message index;

while (i<resposta.length())
{
// assuming all substrings are 4 char long;
message[j] = resposta.substring(i, i+4).toInt();
i = i + 5;
j++;
}

i tested this code, dont work. Terminal said:
char message = {0x01, 0x03, 0x1B, 0x59, 0x00, 0x01, 0x52, 0xFD };
String resposta = “0x01,0x03,0x1B,0x59,0x00,0x01,0x52,0xFD”;
int message2[9] = {0};

bla bla bla

while (i<resposta.length())
{
// assuming all substrings are 4 char long;
message2[j] = resposta.substring(i, i+4).toInt();
i = i + 5;
j++;
}

Serial.println(“Generated”);
Serial.write(message2[9]);
Serial.println("");
Serial.println(“Original”);
Serial.write(message, sizeof(message));
Serial.println("");

Generated
[blank]

Original
[ASCII code]

ok my friend.
Here is the tested (and working) version

``````String resposta = "0x01,0x03,0x1B,0x59,0x00,0x01,0x52,0xFD";

int message[9] = {0};
int i = 0;  // string index;
int j = 0;  // message[] index;
char hexa[5] = {0};

void setup()
{
Serial.begin(9600);

while (i<resposta.length())
{
// assuming all substrings are 4 char long;
String hex = resposta.substring(i, i+4).c_str();
strcpy(hexa, hex.c_str());
Serial.print("hex=");
Serial.println(hexa);
message[j] = (int)strtol(hexa, NULL, 0);
Serial.print("message[");
Serial.print(j);
Serial.print("]=");
Serial.println(message[j]);
i = i + 5;
j++;
}

}

void loop()
{
}
``````

Thanks my friend, your code works.

I make some changes becouse i need to show in ASCII.

char message2[8];

message2[j] = (char)strtol(hexa, NULL, 0);

``````byte message3[] = {0x01, 0x03, 0x1B, 0x59, 0x00, 0x01, 0x52, 0xFD };

String resposta = "0x01,0x03,0x1B,0x59,0x00,0x01,0x52,0xFD";
char message2[8];
int message[9] = {0};
int i = 0;  // string index;
int j = 0;  // message[] index;
char hexa[5] = {0};

void setup()
{
Serial.begin(9600);

while (i<resposta.length())
{
// assuming all substrings are 4 char long;
String hex = resposta.substring(i, i+4).c_str();
strcpy(hexa, hex.c_str());
Serial.print("hex=");
Serial.println(hexa);
message[j] = (int)strtol(hexa, NULL, 0);
message2[j] = (char)strtol(hexa, NULL, 0);
Serial.print("message[");
Serial.print(j);
Serial.print("]=");
Serial.println(message[j]);
i = i + 5;
j++;
}
Serial.write(message2, sizeof(message2));
Serial.println("");
Serial.println("Original byte");
Serial.write(message3, sizeof(message3));
}

void loop()
{
}
``````

Thanks for all.

Best Regards.

João Daniel

Thanks my friend, your code works.

I make some changes becouse i need to show in ASCII.

char message2[8];

message2[j] = (char)strtol(hexa, NULL, 0);

``````byte message3[] = {0x01, 0x03, 0x1B, 0x59, 0x00, 0x01, 0x52, 0xFD };

String resposta = "0x01,0x03,0x1B,0x59,0x00,0x01,0x52,0xFD";
char message2[8];
int message[9] = {0};
int i = 0;  // string index;
int j = 0;  // message[] index;
char hexa[5] = {0};

void setup()
{
Serial.begin(9600);

while (i<resposta.length())
{
// assuming all substrings are 4 char long;
String hex = resposta.substring(i, i+4).c_str();
strcpy(hexa, hex.c_str());
Serial.print("hex=");
Serial.println(hexa);
message[j] = (int)strtol(hexa, NULL, 0);
message2[j] = (char)strtol(hexa, NULL, 0);
Serial.print("message[");
Serial.print(j);
Serial.print("]=");
Serial.println(message[j]);
i = i + 5;
j++;
}
Serial.write(message2, sizeof(message2));
Serial.println("");
Serial.println("Original byte");
Serial.write(message3, sizeof(message3));
}

void loop()
{
}
``````

Thanks for all.

Best Regards.

João Daniel