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
    #1
    1. Advertising

  2. Eric Sosman Guest

    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
    #2
    1. Advertising

  3. Lew Pitcher Guest

    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
    #3
  4. Willem Guest

    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
    #4
  5. <> 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
    #5
  6. 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
    #6
  7. jacob navia Guest

    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
    #7
  8. jacob navia Guest

    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
    #8
  9. 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 Dijk, Jan 14, 2008
    #9
  10. jacob navia wrote:

    [advertisement for proprietary software snipped]

    Please grow up.
     
    Martin Ambuhl, Jan 14, 2008
    #10
  11. jacob navia wrote:

    [advertisement for expropriators software snipped]

    Please grow up.
     
    Martin Ambuhl, Jan 14, 2008
    #11
  12. Ben Pfaff Guest

    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 1:putchar(a[i&15]);break;}}}
     
    Ben Pfaff, Jan 14, 2008
    #12
  13. In article <>,
    Martin Ambuhl <> wrote:
    >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"))

    --
    "History is a pile of debris" -- Laurie Anderson
     
    Walter Roberson, Jan 14, 2008
    #13
  14. jacob navia Guest

    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
    #14
  15. 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)

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Jan 15, 2008
    #15
  16. 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
    #16
  17. CBFalconer Guest

    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>
    Try the download section.


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 15, 2008
    #17
  18. Harald van Dijk <> 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
    #18
  19. 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
    #19
  20. Mark Bluemel Guest

    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
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles T.

    Binary data representation

    Charles T., Feb 4, 2004, in forum: C++
    Replies:
    11
    Views:
    838
    Charles T.
    Feb 5, 2004
  2. Mark Dufour
    Replies:
    0
    Views:
    313
    Mark Dufour
    Dec 16, 2003
  3. Harry George
    Replies:
    1
    Views:
    357
    Fredrik Lundh
    Dec 16, 2003
  4. Mark Dufour
    Replies:
    5
    Views:
    380
    Bengt Richter
    Dec 18, 2003
  5. Rim
    Replies:
    3
    Views:
    1,115
    Dan Bishop
    Jan 27, 2004
Loading...

Share This Page