conversion string to integer

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello

As said in the tittle
I got problems to convert an usart receipt string to an integer...

time_request=atoi(rx);

where rx is

volatile char rx[6];

Do atoi() is the good function??

Thanks

computer : GNU Linux / e6750
ship : Attiny 2313 / ATmega8
compiler : avr-gcc 1:4.3.2-1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes it's the right function to use but note that it stops converting the moment it hits a non numeric so you need to position it on a numeric (or sign character) to start with:

char rx[] = { "test12345end"};
char *p = &rx
while ((*p<'0') || (*p>'9')) {
 p++;
}
//here p points to the '1'
time=atoi(p);
// time now holds 12345

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, clawson.
I study your stuff, i'm near my goal.
Thanks for itoa() précision (this isn't said in the doc online).

computer : GNU Linux / e6750
ship : Attiny 2313 / ATmega8
compiler : avr-gcc 1:4.3.2-1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

zettaman wrote:
Thanks for itoa() précision (this isn't said in the doc online).
Umm, actually it is. The prototype for atoi is
int atoi ( const char * s )

In the AVR, int is a 16-bit (2 byte) value. A long is 32-bits (4 bytes), and the function to convert a string to a long is atol().

All is revealed in the Avr-libc General Utilities age.

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!