Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to get binary representation of c objects ?

Reply
Thread Tools

how to get binary representation of c objects ?

 
 
davidComSee@gmail.com
Guest
Posts: n/a
 
      01-14-2008
Hi all:

char c = 'a';

To get the hex version of the byte c, i can simply use:

printf( " %x ",c);

But i couldn't find a control letter for binary output.

Thanks
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      01-14-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all:
>
> char c = 'a';
>
> To get the hex version of the byte c, i can simply use:
>
> printf( " %x ",c);
>
> But i couldn't find a control letter for binary output.


See Question 20.10 in the comp.lang.c Frequently Asked
Questions (FAQ) list at http://www.c-faq.com/.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      01-14-2008
On Jan 14, 3:39 pm, (E-Mail Removed) wrote:
> Hi all:
>
> char c = 'a';
>
> To get the hex version of the byte c, i can simply use:
>
> printf( " %x ",c);
>
> But i couldn't find a control letter for binary output.


That's because there is no conversion format to output in base 2.


 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      01-14-2008
(E-Mail Removed) wrote:
) Hi all:
)
) char c = 'a';
)
) To get the hex version of the byte c, i can simply use:
)
) printf( " %x ",c);
)
) But i couldn't find a control letter for binary output.

As far as I know, there isn't one. Maybe there are some
implementations that have binary as an extension, but I don't know any.

You're going to have to roll your own if you really want binary output.

Here's a gratuitous example of how not to do it:

int print_bin(unsigned long num, int size)
{
return (size == 1)
? printf("%d", (num & 1))
: print_bin(num >> (size/2), size - size/2)
+ print_bin(num, size/2);
}


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      01-14-2008
<(E-Mail Removed)> wrote in message news
>
> char c = 'a';
>
> To get the hex version of the byte c, i can simply use:
>
> printf( " %x ",c);
>
> But i couldn't find a control letter for binary output.
>

Binary output isn't used often enough to have its own format.
You can convert from hex because each hex digit has 4 corresponding binary
bits

eg F1ED = | 1111 | 0001 | 1110 | 1101

In fact the hex form is far more readable.

It is easy enough to write a dump routine. Use the AND. OR and bitshift
operators to extract the bits for each char. Any object can be treated as an
array of unsigned chars.
For extra brownie points remember that char is not always 8 bits.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-14-2008
(E-Mail Removed) wrote:
> Hi all:
>
> char c = 'a';
>
> To get the hex version of the byte c, i can simply use:
>
> printf( " %x ",c);


No, you can't. c may be signed. If c were declared as unsigned char,
then you could do that.

> But i couldn't find a control letter for binary output.


There isn't one. There are almost no real uses for one, other than as
an elementary programming exercise. Learn to recognize bit patterns
3-bits-at-a-time with octal or 4-bits-at-a-time with hex.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-14-2008
(E-Mail Removed) wrote:
> Hi all:
>
> char c = 'a';
>
> To get the hex version of the byte c, i can simply use:
>
> printf( " %x ",c);
>
> But i couldn't find a control letter for binary output.
>
> Thanks


If you are using lcc-win you can write
#include <stdio.h>
int main(void)
{
printf("%b\n",12345);
}
Output:
11000000111001

P.S. flames >/dev/null

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-14-2008
Willem wrote:
> (E-Mail Removed) wrote:
> ) Hi all:
> )
> ) char c = 'a';
> )
> ) To get the hex version of the byte c, i can simply use:
> )
> ) printf( " %x ",c);
> )
> ) But i couldn't find a control letter for binary output.
>
> As far as I know, there isn't one. Maybe there are some
> implementations that have binary as an extension, but I don't know any.
>


lcc-win implements %b
#include <stdio.h>
int main(void)
{
printf("%b\n",12345);
}
Output:
11000000111001



--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      01-14-2008
On Mon, 14 Jan 2008 17:12:08 -0500, Martin Ambuhl wrote:
> (E-Mail Removed) wrote:
>> Hi all:
>>
>> char c = 'a';
>>
>> To get the hex version of the byte c, i can simply use:
>>
>> printf( " %x ",c);

>
> No, you can't. c may be signed. If c were declared as unsigned char,
> then you could do that.


unsigned char usually promotes to signed int, so you'd be left with the
exact same problem. C99 allows you to use %hhx to print an unsigned char
as a hexadecimal number, but in C90, you need a cast.

Alternatively, you could rely on the almost guaranteed behaviour of
printing a signed int using a format specifier for unsigned int, exactly
as done in the above code. While it _is_ invalid, it's considerably more
complicated to create a conforming implementation where it won't work,
than one where it will, and I don't believe it's been done so far.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-14-2008
jacob navia wrote:

[advertisement for proprietary software snipped]

Please grow up.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Binary data representation Charles T. C++ 11 02-05-2004 04:06 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