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

Converting ASCII characters to values....

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








Converting ASCII characters to values....
PostPosted: Tue May 26, 2009 9:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 26, 2009 9:32 pm     Reply with quote

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








PostPosted: Wed May 27, 2009 3:33 pm     Reply with quote

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
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