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

ATOI() Won't Convert Hex To Int8

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



Joined: 18 Jan 2006
Posts: 43

View user's profile Send private message

ATOI() Won't Convert Hex To Int8
PostPosted: Fri Jun 02, 2006 3:28 pm     Reply with quote

I really did look around here for a solution to this
problem before I posted but...

If I run this code I get X=0.
If I use onle decimal characters it works OK.

int1 temp;
char tmp_buff[15];

int8 x;

tmp_buff[0]='F';
tmp_buff[1]=0;

x=atoi(tmp_buff);

What am I doing wrong ?
The goal is to convert two incoming ASCII checksum characters into int8.
An alternate method idea is welcomed.

Thanks in advance.
Don
Shocked
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Fri Jun 02, 2006 3:31 pm     Reply with quote

From good, ole Unix man pages:

The atoi(), atol(), strtol(), and strtoul() functions are used to convert a
character string pointed to by the nptr parameter to an integer having a
specified data type. The atoi() and atol() functions convert a character
string containing decimal integer constants, but the strtol() and strtoul()
functions can convert a character string containing a integer constant in
octal, decimal, hexadecimal, or a base specified by the base parameter.


Check to see if strtol() and strtoul() exist in the CCS compiler documentation. Otherwise consider writing your own.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
DonWare



Joined: 18 Jan 2006
Posts: 43

View user's profile Send private message

ccs manual
PostPosted: Fri Jun 02, 2006 3:36 pm     Reply with quote

The ccs manual says "Accepts both decimal and hexadecimal argument"

I entered the example form the manual and it worked ok - except for hex input.

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 02, 2006 3:45 pm     Reply with quote

You're giving atoi a string of "F".

How does atoi know that this is a hex digit ?

Look in the source code for atoi(), which is in stdlib.h
(Look it c:\Program Files\PICC\drivers)

It's looking for "0x" to be in front of the hex string:
Code:

      // Check for hex number
      if (c == '0' && (s[index] == 'x' || s[index] == 'X'))
      {


So you need to add two bytes to your tmpbuff to make
it hold "0xF".
DonWare



Joined: 18 Jan 2006
Posts: 43

View user's profile Send private message

Tried
PostPosted: Fri Jun 02, 2006 3:49 pm     Reply with quote

I tried 0x and had no luck.

What I've decided to do is use isalpha() or isdigit() and go from there for each character.

Thanks.
DonWare



Joined: 18 Jan 2006
Posts: 43

View user's profile Send private message

My Bad
PostPosted: Fri Jun 02, 2006 3:52 pm     Reply with quote

I tried 0X again and it worked. Who knows what I did wrong before.

I didn't know code was available for the built in functions.

Thanks alot !!!
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