Go Down

Topic: convert a char array of numbers into an integer (Read 35232 times) previous topic - next topic


Hello everyone,

I am having a rather noobish moment right now and could really use some help.

like the title says, I am trying to convert a (section of a) character array of numbers into an integer.

for example, assume I have the following:

int pos;
char command[4] = "b122"
(can also be represented by: char command[4] = {'b','1','2','2'})

and assume that I want to convert the number section of that array into an actual 3-digit integer where i could set

pos = 122;

Essentially take command[1], command[2] and command[3] and somehow concatenate them and convert into an integer.  Is this possible? If so, how should I go about doing this? :-/ Thanks.

The reason for this is to allow my arduino to take commands from a PC.  From "b122" the 'b' specifies which servo to be accessed (in this case the 'base' servo), and 122 specifies the angle to be set.


Jun 24, 2010, 08:27 am Last Edit: Jun 24, 2010, 08:28 am by iard Reason: 1
I think that you need to use atoi().

Code: [Select]

char buffer[4];
buffer[0] = command[1];
buffer[1] = command[2];
buffer[2] = command[3];
buffer[3] = '\0';

int  n;
n = atoi(buffer);


The first form, 'char command[4] = "b122;"', would generate a compiler error, "error: initializer-string for array of chars is too long", since you've not left room for the terminating zero required for 'C' style strings.  You'd be better off using 'char command[] = "b122";' in which case you could simply do:

Code: [Select]
char command[] = "b122;

int pos = atoi(&command[1]);


Jun 24, 2010, 08:56 am Last Edit: Jun 24, 2010, 09:08 am by Dustin_Dobransky Reason: 1
Wonderful! It worked! Thanks a million! :D ;D :)
I had no idea you could just use 'atoi' on a character array.

...my 'plan B' was to use  the WString library, and convert the character array into a string, then the string into an integer...totally inefficient and space consuming, especially since I only have an atmega168 with my board :P


Thanks for your input also, It works perfectly also.
just a quick question though.  i can see that atoi(&command[1]) will convert all characters from the second position in the array onwards, but is there anyway to start the conversion at for example command[1] and stop it at command[3]?...just curious, Thanks.


Isn't that whats happening now?  Or do you mean as command[]  contained "b12"?

If you don't mind modifying the contents of 'command' you could do:

char command[] = "b122";
command[3] = 0;
int pos = atoi(&command[1]);

And you get pos = 12.


What i meant was if you arbitrarily had for example:

char command[] = "b123456789"

would there be a way to extract '1234' from that array, where you start the conversion to an int at command[1] and end it at command[4], but still keep all the data in tact.  Using a buffering method like iard stated would work, but im just wondering if there is any spectial syntax to do this...

like something that might look along the lines of: atoi(&command[1],command[4])


To do what you want, it would be simplest (probably) to keep the source string intact, and use "strncpy" to copy the portions you want into a temporary buffer, and then apply "atoi" to the temporary buffer.
Per Arduino ad Astra

Go Up