how to get binary representation of c objects ?

D

davidComSee

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
 
L

Lew Pitcher

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

Willem

(e-mail address 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
 
M

Malcolm McLean

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

Martin Ambuhl

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

jacob navia

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
 
J

jacob navia

Willem said:
(e-mail address 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
 
H

Harald van Dijk

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

Martin Ambuhl

jacob navia wrote:

[advertisement for proprietary software snipped]

Please grow up.
 
M

Martin Ambuhl

jacob navia wrote:

[advertisement for expropriators software snipped]

Please grow up.
 
B

Ben Pfaff

jacob navia said:
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?
 
W

Walter Roberson

jacob navia wrote:
[advertisement for proprietary software snipped]
Please grow up.

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
such system. His reply was direct and to the point William had made;
I would not consider it an "advertisement" at all.

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
you have said that my reply was an "advertisement for proprietary
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"))
 
J

jacob navia

Ben said:
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.
 
T

Tomás Ó hÉilidhe

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)
{
char unsigned mask = 1;

do *p++ = x & mask ? '1' : '0';
while (mask <<= 1);

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

CBFalconer

jacob said:
.... 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.
 
K

Keith Thompson

Harald van Dijk said:
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.
 
J

Joachim Schmitz

jacob said:
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
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top