Converting Serial ASCII data into an integer

Hello,

I am trying to convert multi byte serial ASCII data into an integer.

At the moment I am storing my a received ASCII serial data into INT so for example: -

int received[4]

received[0] = 48
received[1] = 50
received[2] = 48
received[3] = 48
received[4] = 48

This converts to from ASCII to 04000.

I would like to convert this into 4000 and store into an integer of it’s own. For Example: -

int convertedNumber = 4000;

I have tried a couple of methods - toInt() & atol() and String to Int conversions but so far I have been unsuccessful.

Could anyone give me any pointers? (No pun intended ;-))

Thanks in advance.

If you use a char array rather than an int array, then you can easily use the provided atoi function.

int received[4]

received[0] = 48
received[1] = 50
received[2] = 48
received[3] = 48
received[4] = 48

A four element array doesn't have an element with the index four.
Perhaps that's part of your problem

it should be like this:

char received[6];

received[0] = 48;
received[1] = 50;
received[2] = 48;
received[3] = 48;
received[4] = 48;
received[5] = '\0';

int value = atoi(received);

received[0] = 48
received[1] = 50
received[2] = 48
received[3] = 48
received[4] = 48

This converts to from ASCII to 04000.

Actually it doesn't

arduino_new:
it should be like this:

char received[6];

received[0] = 48;
received[1] = 50;
received[2] = 48;
received[3] = 48;
received[4] = 48;
received[5] = '\0';

int value = atoi(received);

Great works well - Thanks