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 07:55
Modified:
  04/16/09 07:58

Read: times


 
#164648 - Base conversion.
Responding to: ???'s previous message
I could find some codes that make this, but could not really understand what they do, or why they do that way, I would like know some teory behind this, to understand, google dont says much specifically about this, someone can give some help to my neurons?

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

The most important thing to know is that the digits (dn...d0) of a base-10 number represent the value of the number in the following way: value = (10^0)*d0 + (10^1)*d1 + ... + (10^n)*dn. If you know the maximum number of digits that the number will have in base-10 (5 for a 16-bit integer), then it's easy to calculate dn ... d0 iteratively using integer division:

dn = value / (10^n)
value = value - (dn * 10^n)
d(n-1) = value / (10^(n-1))
value = value - (d(n-1) * 10^(n-1))
...
d1 = value / 10^1
value = value - (d1 * 10^1)
d0 = value

 


The nice thing is that this is independent of the base that value uses natively, e.g. it doesn't matter whether the "system" represents numbers in base-2, base-10, base-16 or base-371 (*shudder*). It also works for conversion to other representations than base-10 - just replace the 10 by the desired base (and remember that number of digits may change, too).


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