To convert a string "1,2,3,4,5,6" to a vector int[]= {1,2,3,4,5,6}

Good morning,

I have a question about to convert a string "1,2,3,4,5,6" to a vector int= {1,2,3,4,5,6}.

I am programming my arduino mega, and I want to convert for example this "10,1,23,2" to a int enteros={10,1,23,2}, I'm a bit confused because I want a simple way to make the conversion, and as in the example in some cases there are two digits and in others only 1 digit.

Any idea is welcome.

PD: sorry for my bad english :slight_smile:

Where does the "10,1,23,2" come from?

Assuming your string is a nul terminated character array, you can use strtok() to split on the commas. Each time a comma is found, strtok() will give you a pointer to a piece of the string (10, next one 1, next one 23 etc).
You can convert these pieces to integers using atoi() or strtol() and store them in the enteros array.

You can convert these pieces to integers using atoi() or strtol() and store them in the enteros array.

You MUST know how many pieces there will be, so that you can size the enteros array correctly. Don't expect to use a vector on the Mega.