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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/22/05 18:37
Read: times


 
#99854 - One size fits all
Responding to: ???'s previous message
Vignesh Prasad said:
Dan, I went thru your code for unsigned short to string function. How about your code for unsigned int to string conversion? I wish to have a look into that(curiosity!!).

I used tostr()+rpt() combination because, I thought in future, I may use something like unsigned long to string conversion, for which I need 1000000,100000,10000, and so on.

All you need to do is change the type of the 'bin' parameter and, in fact, since you mentioned the possibility of an unsigned long version, the following is the only unsigned version that is necessary unless you're really concerned about speed. You see, with this version, an unsigned int value or unsigned char value can be used as the 'bin' parameter without needing special cases. One size fits all.
/****************************************************************************
 *
 *    NAME: UlToStr
 * PURPOSE: Convert unsigned long to decimal string.
 *
 * SYNOPSIS:   void UlToStr(char *s, unsigned long bin, unsigned char n);
 *
 * DESCRIPTION:
 *
 *     The function stores a NUL-terminated string, in "n" + 1 (plus 1 for
 *     the NUL string terminator) successive elements of the array whose
 *     first element has the address "s", by converting "bin" to "n" decimal
 *     characters.
 *
 ****************************************************************************/

void UlToStr(char *s, unsigned long bin, unsigned char n)
{
    s += n;
    *s = '';

    while (n--)
    {
        *--s = (bin % 10) + '0';
        bin /= 10;
    }
}


List of 18 messages in thread
TopicAuthorDate
Keil C51-optimization & Pointers            01/01/70 00:00      
   Bad array index            01/01/70 00:00      
      oops!!            01/01/70 00:00      
         buff[] is auto, not static            01/01/70 00:00      
            Back to basics            01/01/70 00:00      
               Don't shoot the pianist!            01/01/70 00:00      
   Hex file size            01/01/70 00:00      
      Optimization pointers            01/01/70 00:00      
         sprintf() is fairly large            01/01/70 00:00      
         So start a new thread, then!            01/01/70 00:00      
         pls...help            01/01/70 00:00      
            I disagree            01/01/70 00:00      
      My dear Watson !            01/01/70 00:00      
         One size fits all            01/01/70 00:00      
   bin = hex / 2.8            01/01/70 00:00      
      hexmap            01/01/70 00:00      
      A bit of a sweeping generalisation!!            01/01/70 00:00      
   Keil C51-optimization & Pointers            01/01/70 00:00      

Back to Subject List