atoi issues

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
ventete
Posts: 20
Joined: 26 Mar 2011 21:09

atoi issues

#1 Post by ventete » 11 Jan 2016 02:06

Could someone tell me what I'm doing wrong with this simple code???
I can't seem to get atoi to perform the way I expect.

Thank you, Marc


/*
I'm running this program on a PIC18F450 at 20MHz using the
MikroC compiler



*/
unsigned short result;

char teststring[3]={0,0,0};
char uart_rd;

void main() {



UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize

UART1_Write_Text("Start");
UART1_Write(10);
UART1_Write(13);

while (1) {
if (UART1_Data_Ready()) {
uart_rd = UART1_Read();
result=atoi(uart_rd);// atoi is the function I'm having trouble with
ByteToStr(result,teststring);
UART1_Write(uart_rd); //the input character returns correctly here
UART1_Write(10);
UART1_Write(13);
delay_ms(1000);
UART1_Write(teststring); // character out (1-9) does not match
} //the input character
}


}

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: atoi issues

#2 Post by janni » 11 Jan 2016 02:49

ventete wrote:result=atoi(uart_rd);// atoi is the function I'm having trouble with
uart_rd is not a string and thus not a valid parameter of atoi. What is it that you want to achieve?

nico0481
Posts: 6
Joined: 24 Aug 2012 22:00

Re: atoi issues

#3 Post by nico0481 » 11 Jan 2016 09:06

Hi ventete,

In atoi function desription (Help)
Function converts the input string s into an integer value and returns the value
Atoi returns integer, and your "result" is unsigned short.
Could you try to type cast atoi result to unsigned short, like:

Code: Select all

result = (unsigned short) atoi(uart_rd);
See Explicit Types Conversions (Typecasting) in Help

Regards

Nico

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: atoi issues

#4 Post by aCkO » 11 Jan 2016 09:26

nico0481 wrote:Atoi returns integer, and your "result" is unsigned short.
Could you try to type cast atoi result to unsigned short, like:

Code: Select all

result = (unsigned short) atoi(uart_rd);
That won't work because atoi function argument must be a pointer to char, i.e. null terminated array of chars.

The easiest way to convert characters '0' to '9' to their respective integer values is:

Code: Select all

result = uart_rd - '0';
Regards

nico0481
Posts: 6
Joined: 24 Aug 2012 22:00

Re: atoi issues

#5 Post by nico0481 » 11 Jan 2016 09:45

Right! Sorry :?

ventete
Posts: 20
Joined: 26 Mar 2011 21:09

Re: atoi issues

#6 Post by ventete » 11 Jan 2016 21:01

To all:
Thank you for your help. I have solved my atoi issue with all your help. Basically what I am trying to accomplish is to control 2 servos with a joystick and an RS232 link. I've foun that the most efficient way is to send one long string over the link (format: 123456aok0). The 123 is positional info for the first servo and 456 for the second. The alpha character represents positional info for the joystick while the "ok" is the delimiter for the uart receiver.

I then parse the string and convert the chars to ints for further processing.

Now comes the really embarassing part: In the debugger(somehow) the result of the atoi command defaulted to a float output instead of int. In my ignorance of the compiler, I didn't realize that all i had to do was click on the variable in the Watch Window and change it to decimal.

Sorry. And thanks again for your help. Marc

Post Reply

Return to “mikroC PRO for PIC General”