# how to get binary representation of c objects ?

Discussion in 'C Programming' started by davidComSee@gmail.com, Jan 14, 2008.

1. ### Guest

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

, Jan 14, 2008

2. ### Eric SosmanGuest

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
lid

Eric Sosman, Jan 14, 2008

3. ### Lew PitcherGuest

On Jan 14, 3:39 pm, 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.

Lew Pitcher, Jan 14, 2008
4. ### WillemGuest

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

Willem, Jan 14, 2008
5. ### Malcolm McLeanGuest

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

Malcolm McLean, Jan 14, 2008
6. ### Martin AmbuhlGuest

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.

Martin Ambuhl, Jan 14, 2008
7. ### jacob naviaGuest

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

jacob navia, Jan 14, 2008
8. ### jacob naviaGuest

Willem wrote:
> 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

jacob navia, Jan 14, 2008
9. ### Harald van DÄ³kGuest

On Mon, 14 Jan 2008 17:12:08 -0500, Martin Ambuhl wrote:
> 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.

Harald van DÄ³k, Jan 14, 2008
10. ### Martin AmbuhlGuest

jacob navia wrote:

Martin Ambuhl, Jan 14, 2008
11. ### Martin AmbuhlGuest

jacob navia wrote:

Martin Ambuhl, Jan 14, 2008
12. ### Ben PfaffGuest

jacob navia <> writes:

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

The C standards reserve lowercase letters for use in future
versions of the standard library. It also explicitly allows
extensions to use any other characters.

Given that, why did you use a lowercase letter for your
extension?
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}

Ben Pfaff, Jan 14, 2008
13. ### Walter RobersonGuest

In article <>,
Martin Ambuhl <> wrote:
>jacob navia wrote:

I think that remark is unfair to Jacob. William had specifically
said that he did not know of any systems that implement a
binary format specifier extension, and Jacob replied showing one

The fact that Jacob's software is proprietary is irrelevant to the
question: William did not say that he did not know of any "open source"
or "freeware" systems that implement the extension, he said that
he didn't know of *any* systems that had it. If I pointed out
that SGI IRIX's compiler has a %b extension as well, then would
software" ?

(IRIX does have a %b format extension; it just
happens to mean something completely different: it produces
a formatted byte count (e.g., %0.3b would convert 1024 to "1.000kb"))

--
"History is a pile of debris" -- Laurie Anderson

Walter Roberson, Jan 14, 2008
14. ### jacob naviaGuest

Ben Pfaff wrote:
> jacob navia <> writes:
>
>> lcc-win implements %b
>> #include <stdio.h>
>> int main(void)
>> {
>> printf("%b\n",12345);
>> }

>
> The C standards reserve lowercase letters for use in future
> versions of the standard library. It also explicitly allows
> extensions to use any other characters.
>
> Given that, why did you use a lowercase letter for your
> extension?

Obvious: I did not know that. Since this is a very minor extension
maybe I can just change it, but if people use it they will complain

Stupid error from my part.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

jacob navia, Jan 14, 2008
15. ### Tomás Ó hÉilidheGuest

David:

> 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

I suppose you could do something like:

#include <limits.h>

void GetByteAsBinaryString(char unsigned const x,char *p)
{

do *p++ = x & mask ? '1' : '0';

*p = 0;
}

(See the conditional expression for the do-while loop, I realise that if
the operator were << then the resultant expression would have the type
of the promoted-to type. However, with <<=, will it have the promoted-to
type or will it have the original type? The reason I ask is because this
code could malfunction if <<= didn't yield an expression of the original
type)

--
Tomás Ó hÉilidhe

Tomás Ó hÉilidhe, Jan 15, 2008
16. ### Tomás Ó hÉilidheGuest

Tomás Ó hÉilidhe:

> #include <limits.h>

I had originally intended to use CHAR_BIT but didn't end up using it.

--
Tomás Ó hÉilidhe

Tomás Ó hÉilidhe, Jan 15, 2008
17. ### CBFalconerGuest

jacob navia wrote:
>

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

A failure. It should output the string "b", followed by a newline,
and should ignore the "12345". See the C standard for this correct
performance. This does not allow this extension.

You could use the following (tested) routine for binary output:

int pbin(size_t val, FILE *f) {

if (val)
if (pbin(val / 2, f) < 0) return EOF;
return putc((val & 1) + '0', f);
} /* pbin */

and you can also

#define pbinary(i) pbin(i, stdout)

Note that it mimics putc action on i/o error.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

CBFalconer, Jan 15, 2008
18. ### Keith ThompsonGuest

Harald van DÄ³k <> writes:
> On Mon, 14 Jan 2008 17:12:08 -0500, Martin Ambuhl wrote:
>> 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.

Furthermore, the value of 'a' is guaranteed to be positive, even if
plain char is signed.

--
Keith Thompson (The_Other_Keith) <>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jan 15, 2008
19. ### Joachim SchmitzGuest

jacob navia wrote:
> Ben Pfaff wrote:
>> jacob navia <> writes:
>>
>>> lcc-win implements %b
>>> #include <stdio.h>
>>> int main(void)
>>> {
>>> printf("%b\n",12345);
>>> }

>>
>> The C standards reserve lowercase letters for use in future
>> versions of the standard library. It also explicitly allows
>> extensions to use any other characters.
>>
>> Given that, why did you use a lowercase letter for your
>> extension?

>
> Obvious: I did not know that. Since this is a very minor extension
> maybe I can just change it, but if people use it they will complain

For a start emit a warning which tells that %b will get removed and that
(e.g.) %B should be used instead

By, Jojo

Joachim Schmitz, Jan 15, 2008
20. ### Mark BluemelGuest

jacob navia wrote:
> Ben Pfaff wrote:
>> jacob navia <> writes:
>>
>>> lcc-win implements %b
>>> #include <stdio.h>
>>> int main(void)
>>> {
>>> printf("%b\n",12345);
>>> }

>>
>> The C standards reserve lowercase letters for use in future
>> versions of the standard library. It also explicitly allows
>> extensions to use any other characters.
>>
>> Given that, why did you use a lowercase letter for your
>> extension?

>
> Obvious: I did not know that.

On what basis did you develop your compiler and library system, if you
can't use the standard?

Mark Bluemel, Jan 15, 2008