home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16090 < prev    next >
Encoding:
Text File  |  1992-11-07  |  1.3 KB  |  41 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!destroyer!sol.ctr.columbia.edu!eff!world!tombaker
  3. From: tombaker@world.std.com (Tom A Baker)
  4. Subject: Re: (SOLUTION) How to print an integer as binary?
  5. Message-ID: <BxAn1s.32t@world.std.com>
  6. Organization: Me, at The World Public Access UNIX, Brookline, MA
  7. References: <1992Nov4.180622.6568@csd.uwe.ac.uk>
  8. Date: Fri, 6 Nov 1992 12:08:15 GMT
  9. Lines: 30
  10.  
  11. In article <1992Nov4.180622.6568@csd.uwe.ac.uk> th2_oate@csd.uwe.ac.uk (Tom Oates) writes:
  12.  
  13. TO> Assuming that:- 
  14. TO> 
  15. TO> int i = 203;
  16. TO> 
  17. TO> How do I display 'i' as 11001011?
  18.  
  19.   I am amazed at the timing, a coincidence ... I've just completed a
  20. package to do this, and used it on a  project on Forward Error
  21. Correcting Codes.  It provides for constants (either "11001011",
  22. "b11001011", or "0b11001011" (I prefer the last)), and converting
  23. to and from strings to and from integers, and to floats (1-1/2
  24. becomes "1.1" in binary).
  25.  
  26.   Once I put it together this weekend, I'll be glad to mail it to
  27. requestors, and may pop it onto comp(or *).sources .
  28.  
  29.   Incidentally, the string routine you specifically asked about uses
  30. the algorithm:
  31.  
  32.     1) Convert to octal string.
  33.  
  34.     2) Convert THAT to binary characters.
  35.  
  36.     3) Use a "while()" loop to get rid of the leading zeros.
  37.  
  38. tombaker@world.std.com
  39.  
  40.  
  41.