Go Down

Topic: Converting from String to Char Array (Read 292 times) previous topic - next topic

Alex_Z

Hi all!

I would like to convert this string that I will send over from Processing to Arduino "0xB20000 0x000DFF"

How do I convert it to Char Array so I can use it for instance like

Colour[0] in Arduino which will give me "0xB20000"
Colour[1] in Arduino which will give me "0x000DFF"

I have got Serial running already

String str;
str = Serial.readString();

Thanks alot in advance!

sterretje

#1
May 22, 2016, 07:02 pm Last Edit: May 22, 2016, 07:02 pm by sterretje
Get rid of String (capital S) and use one of the approaches in serial input basics - updated to directly get data into a character array.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

UKHeliBob

https://www.arduino.cc/en/Reference/StringToCharArray
You could, of course, work with char arrays in your program to avoid the need to convert from String to string
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Alex_Z

Get rid of String (capital S) and use one of the approaches in serial input basics - updated to directly get data into a character array.
Does this still work if I'm using Processing to extract data from a JSON file?

https://www.arduino.cc/en/Reference/StringToCharArray
You could, of course, work with char arrays in your program to avoid the need to convert from String to string
I'm not sure how to use that...

What am I doing wrong here?

char inChar;
char inData;
String str;

str = Serial.readString();
int str_len = str.length() + 1;
char char_array[str_len];

char_array[0] should give me 0xB20000
char_array[1] should give me 0x000DFF
right?

UKHeliBob

Code: [Select]
char char_array[str_len];Declare an array of chars but don't initialise it.

Quote
char_array[0] should give me 0xB20000
char_array[1] should give me 0x000DFF
Why would it ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

sterretje

#5
May 22, 2016, 07:16 pm Last Edit: May 22, 2016, 07:17 pm by sterretje
char_array[0] is the first element of char_array
char_array[1] is the second element of char_array

The first element is '0', the second element is 'x'.

Serial communication has no idea about how the data is created (String, binary stuff, character array). So yes, the Serial Input Basics - updated thread will work; even contains an example how to parse (split) the data.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Alex_Z

char_array[0] is the first element of char_array
char_array[1] is the second element of char_array

Serial communication has no idea about how the data is created (String, binary stuff, character array). So yes, the Serial Input Basics - updated thread will work; even contains an example how to parse (split) the data.

I looked at the thread and have no idea how to even start...

I'm very new to Arduino..

Is there no easier way?

Processing sends data over Serial "0xB20000 0x45cfc9 0x95b8e3"
Arduino strings them into str and then does more to process them in such a way that

Colour[1] = 0xB20000
Colour[2] = 0x45cfc9
Colour[3] = 0x95b8e3

?

Or even with what I have now

I tried using char_array[0, 7], still does not give me 0xB20000

Go Up