View previous topic :: View next topic |
Author |
Message |
DonWare
Joined: 18 Jan 2006 Posts: 43
|
ATOI() Won't Convert Hex To Int8 |
Posted: Fri Jun 02, 2006 3:28 pm |
|
|
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
|
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Fri Jun 02, 2006 3:31 pm |
|
|
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
|
ccs manual |
Posted: Fri Jun 02, 2006 3:36 pm |
|
|
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
|
|
Posted: Fri Jun 02, 2006 3:45 pm |
|
|
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
|
Tried |
Posted: Fri Jun 02, 2006 3:49 pm |
|
|
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
|
My Bad |
Posted: Fri Jun 02, 2006 3:52 pm |
|
|
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 !!! |
|
|
|