home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / base.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  1.4 KB  |  50 lines

  1. /* ACE linked library module: BIN$, OCT$ 
  2. ** Copyright (C) 1998 David Benn
  3. ** 
  4. ** This program is free software; you can redistribute it and/or
  5. ** modify it under the terms of the GNU General Public License
  6. ** as published by the Free Software Foundation; either version 2
  7. ** of the License, or (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  
  18.    Author: David J Benn
  19.      Date: 29th June 1993
  20. */
  21.  
  22. char *base(n,buf,radexp)
  23. long n;
  24. char *buf;
  25. long radexp;
  26. {
  27. long q,r,i,cc=-1;
  28. static char tmp[32];
  29. /* convert a long integer to a 
  30.    string of base 2^radexp */
  31.  
  32.  if (n == 0L) 
  33.     { buf[0] = '0'; buf[1] = '\0'; }
  34.  else
  35.  {
  36.   while (n != 0)
  37.   {
  38.    q = n >> radexp;        /* quotient = number div 2 */
  39.    r = n - (q << radexp);     /* remainder = quotient mod 2 */
  40.    n >>= radexp;            /* number = number div 2 */
  41.    tmp[++cc] = '0'+r; 
  42.   }
  43.   /* reverse digits */
  44.   for (i=cc;i>=0;i--) buf[cc-i] = tmp[i];
  45.   buf[cc+1] = '\0';
  46.  }           
  47.  
  48.  return(buf);
  49. }
  50.