446,191 Members | 812 Online
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

# Binary output (literally 1's and 0's)

 P: n/a I'm working on a simple hex editing type program and I was wondering if there was any way to display the actual 1's and 0's of binary to the screen. For example if I had some character, 'a' for example, could I print the binary data for it, 01100001? This is a function I would really like to include, for educational purposes, so if there is anyway to do it, I would be be very happy if someone would tell me. Thanks. Nori ------------------------------------------------------------------------------------------------------------------------------------- I RTFM. "The only differnace between Windows and Linux is that Windows is a piece of shit." Nov 4 '06 #1
7 Replies

 P: n/a no*********@gmail.com wrote: I'm working on a simple hex editing type program and I was wondering if there was any way to display the actual 1's and 0's of binary to the screen. You mean, you want to represent binary digits as, say, ASCII characters 0x30 and 0x31? For example if I had some character, 'a' for example, could I print the binary data for it, 01100001? This is a function I would really like to include, for educational purposes, so if there is anyway to do it, I would be be very happy if someone would tell me. Thanks. Nori For each bit in the number that you want to represent, test it with a bitwise AND. Depending on the result of the AND, print your representation for "ONE" or for "ZERO" I personally do not tend to visualize binary numbers as bit strings, but rather, as matrices of boolean values. Nov 4 '06 #2

 P: n/a "no*********@gmail.com" >= 1) { *dest++ = c & mask ? '1' : '0'; } *dest = 0; } #v- -- Best regards, _ _ .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michal "mina86" Nazarewicz (o o) ooo +-------ooO--(_)--Ooo-- Nov 4 '06 #3

 P: n/a no*********@gmail.com wrote: I'm working on a simple hex editing type program and I was wondering if there was any way to display the actual 1's and 0's of binary to the screen. For example if I had some character, 'a' for example, could I print the binary data for it, 01100001? This is a function I would really like to include, for educational purposes, so if there is anyway to do it, I would be be very happy if someone would tell me. Thanks. Nori Something like.. typedef unsigned char uchar; void bits(uchar b, int n) { for (--n; n >= 0; --n) putchar((b & 1 << n) ? '1' : '0'); putchar(' '); } void byte(uchar b) { bits(b, CHAR_BIT); } -- Joe Wright "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- Nov 4 '06 #4

 P: n/a On 4 Nov 2006 14:21:07 -0800, "no*********@gmail.com" I'm working on a simple hex editing type program and I was wondering ifthere was any way to display the actual 1's and 0's of binary to thescreen. For example if I had some character, 'a' for example, could Iprint the binary data for it, 01100001? This is a function I wouldreally like to include, for educational purposes, so if there is anywayto do it, I would be be very happy if someone would tell me. Thanks.Nori For each bit position, starting with the position of most significance and proceeding to the position of least significance, if the bit is zero, output a '0'; otherwise, output a '1'. -- Dan Henry Nov 4 '06 #5