Pages: [1]   Go Down
Author Topic: convert a char array of numbers into an integer  (Read 7623 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I think that you need to use atoi().

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

int  n;
n = atoi(buffer);
« Last Edit: June 24, 2010, 01:28:40 am by iard » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char command[] = "b122;

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wonderful! It worked! Thanks a million! smiley-grin ;D smiley
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 smiley-razz

Edit:

@lloyddean:
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.
« Last Edit: June 24, 2010, 02:08:01 am by Dustin_Dobransky » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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])
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Per Arduino ad Astra

Pages: [1]   Go Up
Jump to: