Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/16/09 08:33
Read: times


 
#164655 - ASCII
Responding to: ???'s previous message
Christoph Franck said:
"Converting" a 16-bit value to ASCII is basically just a change of the base of the number - from base-2 (or base-16) to base-10.

Actually, ASCII is a separate issue from the radix conversion.

ASCII is a code for representing printable characters in computer systems
http://www.asciichart.com/ascii_hexadecimal.html

So converting from a number to displayable text really requires two steps:

1. Determine the value of each digit required in the chosen radix;

2. Determine the ASCII code to represent that digit in the displayable output.

Fortunately, for digits 0-9, the conversion from the numerical value to the ASCII character code is trivial:

Character '0' has ASCII Code 0x30;
Character '1' has ASCII Code 0x31;
Character '2' has ASCII Code 0x32;
Character '3' has ASCII Code 0x33;

You see the pattern - the ASCII code for the digit n can be found simply by adding n to the ASCII code for '0'

unsigned char digit_value; // Numerical value of the digit, 0-9
unsigned char ascii_code;  // ASCII code for the character. '0'-'9'

:

ascii_code = '0' + digit_value;


Note the use of the character constant '0' rather than the magic number 0x30 - this helps to make it clear that we're talking about characters.
It also makes the code portable to any other character-coding scheme (provided that scheme also has its codes for 0-9 in sequence)

It is left as an excercise to the student to consider how this may be extended to give ASCII text representation in Hexadecimal notation...

List of 25 messages in thread
TopicAuthorDate
16bit number to ascii            01/01/70 00:00      
   Base conversion.            01/01/70 00:00      
      Number Base (Radix) and Place Value            01/01/70 00:00      
      ASCII            01/01/70 00:00      
         Right ... I forgot about that part.            01/01/70 00:00      
            What I'd wonder ...            01/01/70 00:00      
               Is it relevant?            01/01/70 00:00      
                  That's somewhat of a description            01/01/70 00:00      
                     D'oh! You've given it away            01/01/70 00:00      
                        Well ... not quite ...            01/01/70 00:00      
                           That was step 1.            01/01/70 00:00      
      not hard when talking in human language or C...            01/01/70 00:00      
         Irrelevant            01/01/70 00:00      
   Op code?            01/01/70 00:00      
      Ah, yes ... the Fairchild FND-70 driver ...            01/01/70 00:00      
         nybble-to-7-segment encoder            01/01/70 00:00      
            if...            01/01/70 00:00      
               30 years ago            01/01/70 00:00      
            That's a BIG if            01/01/70 00:00      
               Serial output            01/01/70 00:00      
                  That might work in SOME cases ...            01/01/70 00:00      
                     But I'm not that fast            01/01/70 00:00      
                        What about infrequently recurring events?            01/01/70 00:00      
         FND-70 driver? Luxury!            01/01/70 00:00      
            Decoding DTMF            01/01/70 00:00      

Back to Subject List