473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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 1890
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
"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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
2
by: anirudhvr | last post by:
Hi, I needed a basic binary to ascii encoder, so I wrote this piece of code: /* Encoding algo: suppose 11111010 is the byte to be encoded. It is first broken up into two 4-bit parts (1111...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
1
by: Michael | last post by:
I have a solution for this, but it feels wrong. If anyone could offer a better one, I'm all ears. (Or technically, eyes.) Basically, I have a bunch of classes. For concreteness, one is a...
3
by: zgfareed | last post by:
My program converts decimal numbers from to binary and hexadecimal. I am having trouble with my output which is supposed to be in a certain format. Binary needs to be in the format of XXXX XXXX...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.