|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Converting ASCII characters to values.... |
Posted: Tue May 26, 2009 9:11 pm |
|
|
Hi All,
I'm transmitting ASCII characters to my PIC via the UART. These characters represent hex bytes, such as 'CF' or 'DE', etc. Is there an compact, easy way to convert these character pairs into a numeric value? I can think of a few brute force methods (Case statements, etc), but nothing elegant...
Suggestions welcome!
Charlie |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 26, 2009 9:32 pm |
|
|
Look at the atoi_b16() function in the CCS loader.c file:
Quote: | c:\program files\picc\drivers\loader.c |
Here is the axtoi() function:
http://www.ccsinfo.com/forum/viewtopic.php?t=35175&start=2
It's a version of atoi() which expects hex characters.
A reason for using axtoi() instead of atoi():
http://www.ccsinfo.com/forum/viewtopic.php?t=28088
Look at the functions in input.c, which allow you to get hex digits, longs,
and floats from the PC:
Quote: | c:\program files\picc\drivers\input.c |
Look at the many functions in stdlib.c, such as strtol, strtoul, etc.
Quote: | c:\program files\picc\drivers\stdlib.h |
Look in the CCS manual in this section for a list of these functions:
Quote: | STANDARD
C CHAR /
STRING |
Possibly use Google to find better and longer explanations than are
given in the CCS manual. |
|
|
Guest
|
|
Posted: Wed May 27, 2009 3:33 pm |
|
|
Hi PCM,
Thanks for the pointers. Actually, the job was simpler than I imagined. Here is the routine I wrote based on your earlier work. It seems to perform just fine.
Code: |
int AsciiToHex(char Digit)
{
//This routine takes an uppercase ASCII hex character (0 - 9) and (A - F) and converts
//the character to a binary representation.
//Here we check if the char is an ASCII hex char
if ((Digit < '0') || (Digit > 'F') || ((Digit < 'A') && (Digit > '9')))
break;
//Here we convert the ASCII hex char to binary and return the value
Digit -= 0x30;
if(Digit > 9)
Digit -= 7;
return(Digit);
}
|
And, I use it like this:
Code: |
MUX0 = (AsciiToHex(RxBuffer[5]) * 16) + (AsciiToHex(RxBuffer[6]));
|
Do you see anything wrong with this implementation?
Thanks,
Charlie |
|
|
|
|
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
|