Hello,
probably a basic question
:
How I can convert from String with example value like this "1,2,3,4,5,6,7,8,9"
to a array from this type:
unsigned int my_array[] = {1,2,3,4,5,6,7,8,9};
Thanks in advance!
Hello,
probably a basic question
:
How I can convert from String with example value like this "1,2,3,4,5,6,7,8,9"
to a array from this type:
unsigned int my_array[] = {1,2,3,4,5,6,7,8,9};
Thanks in advance!
Using the relevant cstring functions and reading Robin' Serial tutorial
(Or replace the quotes with {} )
Why is it in a String in the first place ?
char input[] = "1,2,3,4,5,6,7,8,9";
int output[10];
byte parsed;
void setup() {
Serial.begin(250000);
for (char* pch = strtok (input, ","); pch != NULL; pch = strtok (NULL, ",")) {
output[parsed++] = atoi(pch);
}
Serial.print(F("output["));
Serial.print(parsed);
Serial.print(F("] = { "));
for (byte i = 0; i < parsed; i++) {
Serial.print(output[i]);
Serial.print(F(", "));
}
Serial.println(F("};"));
}
void loop() {}
output[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
const char input[] = "1,2,3,4,5,6,7,8,9";
byte out[9];
sscanf(input,"%d,%d,%d,%d,%d,%d,%d,%d,%d",out,out+1,out+2,out+3,out+4,out+5,out+6,out+7,out+8);
J-M-L:
const char input[] = "1,2,3,4,5,6,7,8,9";
byte out[9];
sscanf(input,"%d,%d,%d,%d,%d,%d,%d,%d,%d",out,out+1,out+2,out+3,out+4,out+5,out+6,out+7,out+8);
but that would only work for a case with a length of 9. Would need rewriting for longer or shorter inputs. Not good!
That matches the requirements
Poor spec, poor answer
First thanks all for the replies and provided solutions.
UKHeliBob:
Why is it in a String in the first place ?
Well because in my case I would like to use MQTT to pass some payload with ready data - but it will be received as string right.
J-M-L:
That matches the requirementsPoor spec, poor answer
freelancer?
Thanks again
but it will be received as string right.
Received from where ?
If the Arduino is receiving the data via a serial interface then it is highly unlikely that it needs to be s String in the first place. Can you please post the code that receives the data.
So here is the setup I have :
ESP8266 subscribed for several topics in MQTT. Based on the topic Controller will send Infra red commands to IR Led.
Client of MQTT is a web page raising the topics with messages.
There are several topics, which are specific and it is easy to send their code.
But I have also a generic(general) topic, and idea here is that I would like to re-send RAW data coming with the topic and inside message.
Let me know if you need more details on this