home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18381 < prev    next >
Encoding:
Internet Message Format  |  1992-12-15  |  921 b 

  1. Path: sparky!uunet!usc!rutgers!mcdhup!src4src!wozzle!alane
  2. From: alane@wozzle.linet.org (J. Alan Eldridge)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Unsigned Char -> 00100110
  5. Message-ID: <06eqVB5w165w@wozzle.linet.org>
  6. Date: 13 Dec 92 17:45:20 GMT
  7. References: <723621396snx@sound.demon.co.uk>
  8. Organization: Disorganization
  9. Lines: 26
  10.  
  11. rob@sound.demon.co.uk (Robert J Barth) writes:
  12.  
  13. > OK, so I'm lazy...
  14. > How do I convert an unsigned char into it's 8 bits ?
  15. > I want to end up with an output of e.g. 00101101 
  16.  
  17. void OutBin(FILE *pf, unsigned char uc)
  18. {
  19.     int  i;
  20.     char sz[ 9 ];
  21.  
  22.     sz[ 8 ] = 0;
  23.  
  24.     for (i = 7; i >= 0; i--) {
  25.         sz[ i ] = '0' + (uc & 1); 
  26.         uc = uc >> 1;
  27.     }
  28.  
  29.     fprintf(pf, "%s", sz);
  30. }
  31.  
  32. Since this took me less than one minute to write, yeah, you are lazy.
  33. But I hope it helps you out, anyway.  
  34.  
  35. alane@wozzle.linet.org (J. Alan Eldridge)
  36. Fido: The University of Walamaloo 1:272/38.473
  37.