CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

the atoi() fuction?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
garyzheng



Joined: 22 Jul 2004
Posts: 25

View user's profile Send private message

the atoi() fuction?
PostPosted: Thu Aug 26, 2004 3:41 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 26, 2004 3:52 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 26, 2004 5:38 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 26, 2004 10:36 pm     Reply with quote

Also note that the number would be 51 and not 15.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group