home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / AHDI / TTDRIVER / TOASCII.C < prev   
Encoding:
C/C++ Source or Header  |  2001-02-09  |  2.1 KB  |  109 lines

  1. /*
  2.  *    01/05/88    From Derek Mui.
  3.  */
  4.  
  5. /*  This routine convert numeric integer to ascii integer */
  6.  
  7. #include    "defs.h"
  8.  
  9. itoa(inword, numbuf)            
  10. UWORD inword;
  11. char numbuf[];
  12. {    
  13.     UWORD temp1, value;
  14.     register int i, j;
  15.     char tmpbuf[10];
  16.     register char *ascbuf;
  17.     
  18.     ascbuf = numbuf;
  19.     i = 0;                /* if the value is non zero  */
  20.  
  21.     if (!inword)    
  22.     *ascbuf++ = '0';
  23.     else {
  24.     value = inword;
  25.     while(value) {
  26.         temp1 = value % 10;        /*  find the remainder    */
  27.         temp1 += 0x0030;        /*  convert to ASCII    */
  28.         tmpbuf[i++] = temp1;    /*  buffer is reverse    */
  29.         value = value / 10;
  30.     }
  31.  
  32.     for (j = i-1; j >= 0; j--)     /* reverse it back    */
  33.         *ascbuf++ = tmpbuf[j];
  34.     }
  35.  
  36.     *ascbuf = 0;            /* end of string mark    */
  37.     return;
  38. }
  39.  
  40.  
  41. ltoa(inword, numbuf)            
  42. long inword;
  43. char numbuf[];
  44. {    
  45.     long temp1, value;
  46.     register int i, j;
  47.     char tmpbuf[10];
  48.     register char *ascbuf;
  49.     
  50.     ascbuf = numbuf;
  51.     i = 0;                /* if the value is non zero  */
  52.  
  53.     if (!inword)    
  54.     *ascbuf++ = '0';
  55.     else {
  56.     value = inword;
  57.     while(value) {
  58.         temp1 = value % 10;        /*  find the remainder    */
  59.         temp1 += 0x0030;        /*  convert to ASCII    */
  60.         tmpbuf[i++] = temp1;    /*  buffer is reverse    */
  61.         value = value / 10;
  62.     }
  63.  
  64.     for (j = i-1; j >= 0; j--)     /* reverse it back    */
  65.         *ascbuf++ = tmpbuf[j];
  66.     }
  67.  
  68.     *ascbuf = 0;            /* end of string mark    */
  69.     return;
  70. }
  71.  
  72.  
  73. /* Convert numeric hex to ascii hex             */
  74. /*    This routine convert binary number to hex value    */
  75. /*    The input buffer must have a zero at the end    */
  76. /*    size = 0 is LONG else WORD            */
  77.  
  78. htoa(invalue, ascbuf, size)            
  79. long    invalue;
  80. char    ascbuf[];
  81. int    size;        
  82. {
  83.     long mask, value;
  84.     int    i,j;            
  85.  
  86.     size = (size) ? 4 : 8;
  87.  
  88.     for(i = 0; i < size; i++)    /* clean up the buffer    */
  89.     ascbuf[i] = '0';
  90.  
  91.     ascbuf[size] = 0;
  92.     
  93.     j = size;
  94.  
  95.     if (invalue) {
  96.     mask = 0x0000000FL;
  97.     for (i = 0; i < size; i++) {
  98.         value = invalue & mask;
  99.         invalue = invalue >> 4;
  100.         if (value >= 0xA)
  101.         ascbuf[--j] = 'A' + (char)(value - 0xa);
  102.         else
  103.         ascbuf[--j] = '0' + (char)value;
  104.       }
  105.     }
  106.     return;
  107. }
  108.  
  109.