View previous topic :: View next topic |
Author |
Message |
garyzheng
Joined: 22 Jul 2004 Posts: 25
|
the atoi() fuction? |
Posted: Thu Aug 26, 2004 3:41 pm |
|
|
I want to use the atoi() function to convert the some data of stream transmitted from RS232 into the constant data.
the test code i wrote:
int result;
char rx_buffer[2];
rx_buffer[0]=5;
rx_buffer[1]=1;
result=atoi(rx_buffer); //result should be 15 or 0x0f?
but why i can not got the right answer
hope some one can help me figure it out |
|
|
kloppy
Joined: 28 Jul 2004 Posts: 32
|
|
Posted: Thu Aug 26, 2004 3:52 pm |
|
|
Try this... (you have to put a char in the rx_buffer)
Code: |
int result;
char rx_buffer[2];
rx_buffer[0]='5';
rx_buffer[1]='1';
result=atoi(rx_buffer); //result should be 15 or 0x0f?
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 26, 2004 5:38 pm |
|
|
Remember that atoi() requires a string. A string is an sequence of ASCII
characters that ends with a special byte of 0x00, as the string terminator.
If you have an array that holds a two-character string, then you must
make the array be 3 bytes long (at least) because you need to allow
space for the 0x00 byte at the end of the string.
Also, if you create a string by setting each byte in the array with a
line of code, you should add one more line to set the string terminator
byte (0x00) at the end. If you don't do this, the atoi() function will
try to keep processing data (by incrementing a pointer) until it finds a
0x00 byte somewhere. ie., if you leave off the string terminator,
atoi() will likely keep processing garbage after the end of your string. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Aug 26, 2004 10:36 pm |
|
|
Also note that the number would be 51 and not 15. |
|
|
|