home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / SMALL_C / ITOU.C < prev    next >
Text File  |  1987-10-04  |  768b  |  24 lines

  1. #include stdio.h
  2. /*
  3. ** itou -- convert nbr to unsigned decimal string of width sz
  4. **         right adjusted, blank filled; returns str
  5. **
  6. **        if sz > 0 terminate with null byte
  7. **        if sz = 0 find end of string
  8. **        if sz < 0 use last byte for data
  9. */
  10. itou(nbr, str, sz)  int nbr;  char str[];  int sz;  {
  11.   int lowbit;
  12.   if(sz>0) str[--sz]=NULL;
  13.   else if(sz<0) sz = -sz;
  14.   else while(str[sz]!=NULL) ++sz;
  15.   while(sz) {
  16.     lowbit=nbr&1;
  17.     nbr=(nbr>>1)&32767;  /* divide by 2 */
  18.     str[--sz]=((nbr%5)<<1)+lowbit+'0';
  19.     if((nbr=nbr/5)==0) break;
  20.     }
  21.   while(sz) str[--sz]=' ';
  22.   return str;
  23.   }
  24.