Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > print binary representation

Reply
Thread Tools

print binary representation

 
 
Carramba
Guest
Posts: n/a
 
      03-24-2007
Hi!

How can I output value of char or int in binary form with printf(); ?

thanx in advance
 
Reply With Quote
 
 
 
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      03-24-2007
Carramba wrote:
> Hi!
>
> How can I output value of char or int in binary form with printf(); ?
>
> thanx in advance


There is no standard format specifier for binary form. You will have
to do the conversion manually, testing each bit from highest to
lowest, printing '0' if it's not set, and '1' if it is.

 
Reply With Quote
 
 
 
 
jaysome
Guest
Posts: n/a
 
      03-24-2007
On Sat, 24 Mar 2007 08:55:36 +0100, Carramba <(E-Mail Removed)> wrote:

>Hi!
>
>How can I output value of char or int in binary form with printf(); ?
>
>thanx in advance


The C Standards do not define a conversion specifier for printf() to
output in binary. The only portable way to do this is to roll your
own. Here's a start:

printf("%s\n", int_to_binary_string(my_int));

Make sure when you implement int_to_binary_string() that it works with
most desktop targets where sizeof(int) * CHAR_BIT = 32 as well on many
embedded targets where sizeof(int) * CHAR_BIT = 16.

Best regards
--
jay
 
Reply With Quote
 
Beej Jorgensen
Guest
Posts: n/a
 
      03-24-2007
Carramba <(E-Mail Removed)> wrote:
>How can I output value of char or int in binary form with printf(); ?


http://c-faq.com/misc/base2.html

http://c-faq.com/misc/hexio.html

HTH,
-Beej

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      03-24-2007

"Carramba" <(E-Mail Removed)> wrote in message
news:4604d5ca$0$488$(E-Mail Removed)...
> Hi!
>
> How can I output value of char or int in binary form with printf(); ?
>
> thanx in advance

#include <limits.h>
/*
convert machine number to human-readable binary string.
Returns: pointer to static string overwritten with each call.
*/
char *itob(int x)
{
static char buff[sizeof(int) * CHAR_BIT + 1];
int i;
int j = sizeof(int) * CHAR_BIT - 1;

buff[j] = 0;
for(i=0;i<sizeof(int) * CHAR_BIT; i++)
{
if(x & (1 << i))
buff[j] = '1';
else
buff[j] = '0';
j--;
}
return buff;
}

Call

int x = 100;
printf("%s", itob(x));

You might want something more elaborate to cut leading zeroes or handle
negative numbers.

 
Reply With Quote
 
Carramba
Guest
Posts: n/a
 
      03-24-2007
Harald van Dijk wrote:
> Carramba wrote:
>> Hi!
>>
>> How can I output value of char or int in binary form with printf(); ?
>>
>> thanx in advance

>
> There is no standard format specifier for binary form. You will have
> to do the conversion manually, testing each bit from highest to
> lowest, printing '0' if it's not set, and '1' if it is.
>

thanx, maybe you have so suggestion or link for further reading on how
to do it?
 
Reply With Quote
 
Carramba
Guest
Posts: n/a
 
      03-24-2007
thanx ! have few questions about this code
Malcolm McLean wrote:
>
> "Carramba" <(E-Mail Removed)> wrote in message
> news:4604d5ca$0$488$(E-Mail Removed)...
>> Hi!
>>
>> How can I output value of char or int in binary form with printf(); ?
>>
>> thanx in advance

> #include <limits.h>
> /*
> convert machine number to human-readable binary string.
> Returns: pointer to static string overwritten with each call.
> */
> char *itob(int x)
> {
> static char buff[sizeof(int) * CHAR_BIT + 1];

why sizeof(int) * CHAR_BIT + 1 ? what does it mean?
> int i;
> int j = sizeof(int) * CHAR_BIT - 1;

why sizeof(int) * CHAR_BIT - 1 ? what does it mean?
>
> buff[j] = 0;
> for(i=0;i<sizeof(int) * CHAR_BIT; i++)
> {
> if(x & (1 << i))
> buff[j] = '1';
> else
> buff[j] = '0';
> j--;
> }
> return buff;
> }
>
> Call
>
> int x = 100;
> printf("%s", itob(x));
>
> You might want something more elaborate to cut leading zeroes or handle
> negative numbers.

 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      03-24-2007
Carramba wrote:
> Harald van Dijk wrote:
> > Carramba wrote:
> >> Hi!
> >>
> >> How can I output value of char or int in binary form with printf(); ?
> >>
> >> thanx in advance

> >
> > There is no standard format specifier for binary form. You will have
> > to do the conversion manually, testing each bit from highest to
> > lowest, printing '0' if it's not set, and '1' if it is.
> >

> thanx, maybe you have so suggestion or link for further reading on how
> to do it?


Others have given code already, but here's mine anyway:

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

void print_char_binary(char val)
{
char mask;

if(CHAR_MIN < 0)
{
if(val < 0
|| val == 0 && val & CHAR_MAX)
putchar('1');
else
putchar('0');
}

for(mask = (CHAR_MAX >> 1) + 1; mask != 0; mask >>= 1)
if(val & mask)
putchar('1');
else
putchar('0');
}

void print_int_binary(int val)
{
int mask;

if(val < 0
|| val == 0 && val & INT_MAX)
putchar('1');
else
putchar('0');

for(mask = (INT_MAX >> 1) + 1; mask != 0; mask >>= 1)
if(val & mask)
putchar('1');
else
putchar('0');
}

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-24-2007
Carramba wrote:
>
> Hi!
>
> How can I output value of char or int in binary form with printf(); ?
>
> thanx in advance


/* BEGIN output from new.c */

1 = 00000001
2 = 00000010
3 = 00000011
4 = 00000100
5 = 00000101
6 = 00000110
7 = 00000111
8 = 00001000
9 = 00001001
10 = 00001010
11 = 00001011
12 = 00001100
13 = 00001101
14 = 00001110
15 = 00001111
16 = 00010000
17 = 00010001
18 = 00010010
19 = 00010011
20 = 00010100

/* END output from new.c */

/* BEGIN new.c */

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

#define STRING "%2d = %s\n"
#define E_TYPE char
#define P_TYPE int
#define INITIAL 1
#define FINAL 20
#define INC(E) (++(E))

typedef E_TYPE e_type;
typedef P_TYPE p_type;

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

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

puts("\n/* BEGIN output from new.c */\n");
e = INITIAL;
bitstr(ebits, &e, sizeof e);
printf(STRING, (p_type)e, ebits);
while (FINAL > e) {
INC(e);
bitstr(ebits, &e, sizeof e);
printf(STRING, (p_type)e, 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
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-24-2007
"Carramba" <(E-Mail Removed)> schrieb im Newsbeitrag
news:46050ac1$0$492$(E-Mail Removed)...
> thanx ! have few questions about this code
> Malcolm McLean wrote:
>>
>> "Carramba" <(E-Mail Removed)> wrote in message
>> news:4604d5ca$0$488$(E-Mail Removed)...
>>> Hi!
>>>
>>> How can I output value of char or int in binary form with printf(); ?
>>>
>>> thanx in advance

>> #include <limits.h>
>> /*
>> convert machine number to human-readable binary string.
>> Returns: pointer to static string overwritten with each call.
>> */
>> char *itob(int x)
>> {
>> static char buff[sizeof(int) * CHAR_BIT + 1];

> why sizeof(int) * CHAR_BIT + 1 ? what does it mean?

If you want to put an in't binary representation ionto a string you need
that much space.
On many implementations sizeof(int) is 4 and CHAR_BIT is 8, so you'd need an
array of 33 chars (including the teminating null byte).

I'd use sizeof(x) instead of sizeof(int), that way you can easily change the
function to work on e.g. long long

>> int i;
>> int j = sizeof(int) * CHAR_BIT - 1;

> why sizeof(int) * CHAR_BIT - 1 ? what does it mean?

Arrays count from 0 to n and the terminating null byte isn't needed , so the
index goes from 0 to 31 (assuming the same sizes as above)

>> buff[j] = 0;
>> for(i=0;i<sizeof(int) * CHAR_BIT; i++)
>> {
>> if(x & (1 << i))
>> buff[j] = '1';
>> else
>> buff[j] = '0';
>> j--;
>> }
>> return buff;
>> }
>>
>> Call
>>
>> int x = 100;
>> printf("%s", itob(x));
>>
>> You might want something more elaborate to cut leading zeroes or handle
>> negative numbers


Bye, Jojo.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary data representation Charles T. C++ 11 02-05-2004 04:06 PM
print format for binary representation Rim Python 3 01-27-2004 08:31 PM
Re: Converting integer to binary representation Mark Dufour Python 5 12-18-2003 05:28 AM
Re: Converting integer to binary representation Harry George Python 1 12-16-2003 02:48 PM
Converting integer to binary representation Mark Dufour Python 0 12-16-2003 02:26 PM



Advertisments