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

N

noridotjabi

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
 
J

james of tucson

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.
 
M

Michal Nazarewicz

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-
 
J

Joe Wright

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);
}
 
D

Dan Henry

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'.
 
J

Jens Thoms Toerring

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
 
P

pete

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 */
 
C

CBFalconer

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 */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top