By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 812 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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" <no*********@gmail.comwrites:
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.
Try searching at groups.google.com. Some simple code:

#v+
void *dec2bin(char *dest, unsigned char c) {
unsigned char mask = 1 << (CHAR_BIT - 1);
for (; mask; mask >>= 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 +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--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"
<no*********@gmail.comwrote:
>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
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

P: n/a
no*********@gmail.com <no*********@gmail.comwrote:
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.
What about something simple as this?

#include <stdio.h>
#include <limits.h>

void print_char_as_bin( int c )
{
unsigned char mask;
for ( mask = 1 << ( CHAR_BIT - 1 ); mask != 0; mask >>= 1 )
putchar( ( unsigned char ) c & mask ? '1' : '0' );
}

int main( void )
{
print_char_as_bin( 'a' );
putchar( '\n' );
return 0;
}

Should be easy to extend to other (integral) types:

void print_int_as_bin( int i )
{
unsigned int mask;
for ( mask = 1U << ( CHAR_BIT * sizeof i - 1 ); mask != 0; mask >>= 1 )
putchar( ( unsigned int ) i & mask ? '1' : '0' );
}

void print_long_as_bin( long int l )
{
unsigned long int mask;
for ( mask = 1UL << ( CHAR_BIT * sizeof l - 1 ); mask != 0; mask >>= 1 )
putchar( ( unsigned long int ) l & mask ? '1' : '0' );
}

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Nov 4 '06 #6

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.
/* BEGIN output from new.c */

'a' is 01100001

/* END output from new.c */

/* BEGIN new.c */

#include <limits.h>
#include <stdio.h>

#define E_TYPE char

typedef E_TYPE e_type;

void bitstr(char *str, const void *obj, size_t n);

int main(void)
{
char e;
char ebits[CHAR_BIT * sizeof e + 1];

e = 'a';
bitstr(ebits, &e, sizeof e);
puts("\n/* BEGIN output from new.c */\n");
printf("'a' is %s\n", ebits);
puts("\n/* END output from new.c */");
return 0;
}

void bitstr(char *str, const void *obj, size_t n)
{
unsigned mask;
const unsigned char *byte = obj;

while (n-- != 0) {
mask = ((unsigned char)-1 >1) + 1;
do {
*str++ = (char)(mask & byte[n] ? '1' : '0');
mask >>= 1;
} while (mask != 0);
}
*str = '\0';
}

/* END new.c */
--
pete
Nov 4 '06 #7

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.
Very basic. See the following:

#ifndef dispbase_h_
#define dispbase_h_

#include <string.h>

/* ============================================ */
/* Mask and convert digit to hex representation */
/* Output range is 0..9 and a..f only */
int tio_hexify(unsigned int value);

/* ========================================= */
/* convert number to string in various bases */
/* 2 <= base <= 16, controlled by hexify() */
/* Output string has ls digit first. */
void tio_basedisplay(unsigned long number, unsigned int base,
char * string, size_t maxlgh);

/* ======================= */
/* reverse string in place */
void tio_revstring(char * string);

#endif

#include "dispbase.h"

/* ============================================ */
/* Mask and convert digit to hex representation */
/* Output range is 0..9 and a..f only */
int tio_hexify(unsigned int value)
{
int result;

result = (value & 0x0f) + '0';
if (result '9')
result = result + ('a' - '0' - 10);
return result;
} /* tio_hexify */

/* ========================================= */
/* convert number to string in various bases */
/* 2 <= base <= 16, controlled by hexify() */
/* Output string has ls digit first. */
void tio_basedisplay(unsigned long number, unsigned int base,
char * string, size_t maxlgh)
{
/* assert (string[maxlgh]) is valid storage */
if (!maxlgh) {
*string = '\0';
return;
}
else {
*string = tio_hexify(number % base);
if (!number) *string = '\0';
else {
tio_basedisplay(number / base, base,
&string[1], maxlgh - 1);
}
}
} /* tio_basedisplay */

/* ======================= */
/* reverse string in place */
void tio_revstring(char * string)
{
char * last, temp;

last = string + strlen(string); /* points to '\0' */
while (last-- string) {
temp = *string; *string++ = *last; *last = temp;
}
} /* tio_revstring */

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Nov 5 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.