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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/08/08 19:32
Read: times


 
Msg Score: +1
 +1 Good Answer/Helpful
#154589 - How the small trick works
Responding to: ???'s previous message
Russ said:
return "0123456789ABCDEF"[HexValue];

Chris said:
Russ, what is that doing?

The quoted string makes a 17-byte array of characters somewhere in memory that contains each of the 16 possible hex digits, followed by a single byte of all zeros that terminates the string. The syntax given then simply returns the element of that array that's indexed by 'HexValue'. The net effect is a mini lookup table, much as if you'd defined the table explicitly, like this:
char hexDigits[] = "0123456789ABCDEF";

return hexDigits[HexValue]; 
This trick is standard C stuff, and works just fine, as you have demonstrated. Whether or not it is actually a good trick is debatable, for at least a couple of reasons:
  • Some would say that it's little bit cryptic, and that's almost never a good thing.

  • On a microcontroller, you need to be aware of where the compiler puts the array. You probably want it in the code space, which is usually more plentiful than RAM and which is not subject to corruption by buggy code. I suppose various compilers would support various options and/or language extensions that would let you control this sort of thing. You wouldn't want to leave it to chance.
-- Russ


List of 35 messages in thread
TopicAuthorDate
Problem with the toascii() w/ clarification            01/01/70 00:00      
   toascii not what you want            01/01/70 00:00      
      Ah yes.....makes sense            01/01/70 00:00      
         C does know ASCII            01/01/70 00:00      
            the final result:            01/01/70 00:00      
               That is a LOT of code            01/01/70 00:00      
                  It won't compile in its state because...            01/01/70 00:00      
               Grossly inefficient!            01/01/70 00:00      
                  A small trick            01/01/70 00:00      
                     small trick is cool.....            01/01/70 00:00      
                        Same as Neil's            01/01/70 00:00      
                           Ah makes sense.....            01/01/70 00:00      
                              You can still do better            01/01/70 00:00      
                                 LOL....Russ.......LOL            01/01/70 00:00      
                                    sprintf()            01/01/70 00:00      
                                       I had you at <>            01/01/70 00:00      
                                          So why not sprintf()?            01/01/70 00:00      
                                             You are right......I did say that            01/01/70 00:00      
                        How the small trick works            01/01/70 00:00      
                           Is it worth it?            01/01/70 00:00      
                              Same assembly generated for me            01/01/70 00:00      
                           You said index.....click click....very nice            01/01/70 00:00      
                              fast (sic?) lookup table            01/01/70 00:00      
                                 Faster? Probably not.            01/01/70 00:00      
               What's in a name?            01/01/70 00:00      
                  true.....but at this point.......            01/01/70 00:00      
                     Tricks and treats            01/01/70 00:00      
                     there is no time lost by doing it right            01/01/70 00:00      
                        You guys are completely right,            01/01/70 00:00      
                           the very simple way to (almost) do it right            01/01/70 00:00      
                           For the sake of learning            01/01/70 00:00      
                              Good point            01/01/70 00:00      
   UART doesn't know ASCII            01/01/70 00:00      
      Good point....but            01/01/70 00:00      
   Unhelpful documentation            01/01/70 00:00      

Back to Subject List