text to binary

Discussion in 'C Programming' started by raghu, Oct 9, 2006.

  1. raghu

    raghu Guest

    how do i convert a text entered through keyboard into a binary format?
    Should I first convert each letter of the text to ASCII and then
    binary???

    Is this method correct? Please advise.

    Thanks a lot.

    Regards,
    Raghu
    raghu, Oct 9, 2006
    #1
    1. Advertising

  2. raghu said:

    > how do i convert a text entered through keyboard


    C doesn't guarantee you a keyboard. Presumably you mean the standard input
    stream.

    > into a binary format?


    Which binary format would you like?

    > Should I first convert each letter of the text to ASCII and then
    > binary???
    >
    > Is this method correct? Please advise.


    You need to be a lot more specific about the nature of your input and the
    desired characteristics of the corresponding output.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Oct 9, 2006
    #2
    1. Advertising

  3. raghu

    raghu Guest

    I mean by using scanf statement if i give the text message (including
    only spaces not special characters) can that be converted to binary 1's
    and 0's.

    Hope now i'm clear

    Richard Heathfield wrote:
    > raghu said:
    >
    > > how do i convert a text entered through keyboard

    >
    > C doesn't guarantee you a keyboard. Presumably you mean the standard input
    > stream.
    >
    > > into a binary format?

    >
    > Which binary format would you like?
    >
    > > Should I first convert each letter of the text to ASCII and then
    > > binary???
    > >
    > > Is this method correct? Please advise.

    >
    > You need to be a lot more specific about the nature of your input and the
    > desired characteristics of the corresponding output.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
    raghu, Oct 9, 2006
    #3
  4. raghu

    osmium Guest

    "raghu" writes:

    > how do i convert a text entered through keyboard into a binary format?
    > Should I first convert each letter of the text to ASCII and then
    > binary???
    >
    > Is this method correct? Please advise.


    No. The phrase "binary file" in the context of the C language was a very
    poor and misleading choice. A binary file is one in which CR and LF have
    the same encoding in an external medium (file) and in RAM. A so-called
    "text" file silently transforms between <CR><LF> or <LF><CR> and '\n' when
    the file is read or written. (Note that 'n' has the same ASCII encoding as
    LF.)

    What is ASCII? It is a formal definition, mapping if you like, between
    certain glyphs in the latin alphabet -mostly - and a binary code. It already
    *is* a binary file, if the word binary is not misused. A file that consists
    entirely of ASCII characters should always be saved and
    retrieved as a non-binary file. With the usual caveats about "always" being
    a troublesome word.

    If this leaves you confused, do a search on google groups, you will find
    older posts discussing this problem..
    osmium, Oct 9, 2006
    #4
  5. "raghu" <> wrote in message
    news:...
    >I mean by using scanf statement if i give the text message (including
    > only spaces not special characters) can that be converted to binary 1's
    > and 0's.
    >


    Don't top post!

    And no, it is not clear what ou want.
    As soon as you accept anything from stdin (no matter what method you use),
    what you read is already "stored" in the computer in binary.

    That is, the computer only contains a whole bunch of ones and zeroes.
    Nothing else. No letters, names, colors, or animals (well, once a ladybug
    did get in my computer, so maybe the latter isn't quite true).

    That's it. Just ones and zeroes.
    Now, certain sequences of ones and zeros are sometimes
    interpreted as representing certain characters.

    So what is it you really want to do with these ones and zeros?

    <snip>
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
    Fred Kleinschmidt, Oct 9, 2006
    #5
  6. "raghu" <> writes:
    > Richard Heathfield wrote:
    >> raghu said:
    >>
    >> > how do i convert a text entered through keyboard

    >>
    >> C doesn't guarantee you a keyboard. Presumably you mean the standard input
    >> stream.
    >>
    >> > into a binary format?

    >>
    >> Which binary format would you like?
    >>
    >> > Should I first convert each letter of the text to ASCII and then
    >> > binary???
    >> >
    >> > Is this method correct? Please advise.

    >>
    >> You need to be a lot more specific about the nature of your input and the
    >> desired characteristics of the corresponding output.

    >
    > I mean by using scanf statement if i give the text message (including
    > only spaces not special characters) can that be converted to binary 1's
    > and 0's.
    >
    > Hope now i'm clear


    Please don't top-post. Read these:
    http://www.caliburn.nl/topposting.html
    http://www.cpax.org.uk/prg/writings/topposting.php

    No, you're not at all clear. You haven't given us any idea of what
    1's and 0's you want.

    Converting the input to Morse Code and representing dots as 0 and
    dashes as 1 would meet the requirements you've stated so far. I'm
    reasonably sure it wouldn't meet your *actual* requirements. Unless
    you tell us what your actual requirements are, we can't help you.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 9, 2006
    #6
  7. raghu

    santosh Guest

    raghu wrote:
    > how do i convert a text entered through keyboard into a binary format?
    > Should I first convert each letter of the text to ASCII and then
    > binary???


    In standard C you can't assume input is from a keyboard and ASCII is
    the character encoding used.

    Besides you not clear at all in specifying what you want to do. If you
    want to convert the input to it's binary representation, (i.e. convert
    'a' to '01100001', assuming ASCII), then this is a trivial job. Convert
    each character of input as it arrives and send it to the desired output
    stream. You can use a lookup table method for the conversion.

    Apart from that, I'm not sure _what_ you're trying to do. Remenber that
    within the computer, _all_ data is binary data.
    santosh, Oct 10, 2006
    #7
  8. raghu wrote:

    > Richard Heathfield wrote:
    >> raghu said:
    >>
    >>> how do i convert a text entered through keyboard

    >> C doesn't guarantee you a keyboard. Presumably you mean the standard input
    >> stream.
    >>
    >>> into a binary format?

    >> Which binary format would you like?
    >>
    >>> Should I first convert each letter of the text to ASCII and then
    >>> binary???
    >>>
    >>> Is this method correct? Please advise.

    >> You need to be a lot more specific about the nature of your input and the
    >> desired characteristics of the corresponding output.
    >>
    >> --
    >> Richard Heathfield
    >> "Usenet is a strange place" - dmr 29/7/1999
    >> http://www.cpax.org.uk
    >> email: rjh at above domain (but drop the www, obviously)

    > I mean by using scanf statement if i give the text message (including
    > only spaces not special characters) can that be converted to binary 1's
    > and 0's.
    >
    > Hope now i'm clear
    >


    No, These values are already, as most other things represented in a
    computer else, a binary value. Perhaps you want to convert it
    to a textual representation of the binary value ?
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Oct 10, 2006
    #8
  9. raghu

    raghu Guest

    int main(void)
    {
    char ch[20];
    printf("enter the text");
    scanf("%s",ch);
    printf("the binary equivalent of the text is\n");
    //here what should be the statement to convert it into binary
    }

    Ex: if i give the text as : google welcomes
    I shud get binary equivalent of each letter as output

    Hope this time i'm very clear....

    Sorry for the inconvenience...
    raghu, Oct 10, 2006
    #9
  10. raghu

    jmcgill Guest

    /* Once again, jmcgill foolishly does someone else's homework */
    #include <stdio.h>
    #include <string.h>

    #define BYTE_BITS 8

    void int2bin(char *out, char b){
    int bits;
    int k;

    bits = BYTE_BITS * sizeof b;

    sprintf(out, "");
    for(k=bits; k>0; k--){
    strncat(out, b & (1 << (k-1) ) ? "1" : "0", 1);
    if(!((k-1)%4)) strncat(out, " ", 1);
    }
    } /* int2bin */

    int main(int argc, char **argv){
    char out[33];
    char in[]="Hello, world!";
    int i;
    for(i=0; i<strlen(in); i++){
    int2bin(out, in);
    printf("'%c' == 0x%02X == %sb\n", in, in, out);
    }
    return 0;
    } /* main */
    jmcgill, Oct 10, 2006
    #10
  11. raghu

    santosh Guest

    raghu wrote:
    > int main(void)
    > {
    > char ch[20];
    > printf("enter the text");
    > scanf("%s",ch);
    > printf("the binary equivalent of the text is\n");
    > //here what should be the statement to convert it into binary
    > }
    >
    > Ex: if i give the text as : google welcomes
    > I shud get binary equivalent of each letter as output
    >
    > Hope this time i'm very clear....
    >
    > Sorry for the inconvenience...


    Please quote the post to which you're replying. Not all readers of
    Usenet can have easy or permanent access to previous posts and without
    context, your reply makes little sense. If you're using Google Groups
    then read the following URLs for more information on how to do it.

    <http://cfaj.freeshell.org/google/>
    <http://www.safalra.com/special/googlegroupsreply/>
    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    <http://en.wikipedia.org/wiki/USENET>

    Coming to your question, as I mentioned in my other post in this
    thread, you'll need to write a function to assemble a base two based
    string representation of your input, character at a time. Such a
    function is a trivially easy task.

    Using CHAR_BIT declared in <limits.h> you can extract each nibble in
    the input character and use it as an offset into a lookup table to
    extract the binary string equivalent, which will be four bytes long.
    You can concactenate these pieces and display the final result to your
    output.

    Have a go at an attempt and if you don't get it right, post the code
    here for further comments.
    santosh, Oct 10, 2006
    #11
  12. raghu wrote:
    > int main(void)
    > {
    > char ch[20];
    > printf("enter the text");
    > scanf("%s",ch);
    > printf("the binary equivalent of the text is\n");
    > //here what should be the statement to convert it into binary
    > }
    >
    > Ex: if i give the text as : google welcomes
    > I shud get binary equivalent of each letter as output
    >
    > Hope this time i'm very clear....

    Use a loop, and perhaps something like:

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

    void putbin(char c)
    {
    int i;

    for(i = CHAR_BIT - 1 ; i >= 0 ; i++)
    putchar((c & 1 << i) ? '1' : '0');
    }
    }
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Oct 10, 2006
    #12
    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. Fangs
    Replies:
    3
    Views:
    9,766
    darshana
    Oct 26, 2008
  2. Marc Schellens
    Replies:
    8
    Views:
    2,985
    John Harrison
    Jul 15, 2003
  3. Replies:
    12
    Views:
    566
    Richard Heathfield
    Apr 8, 2007
  4. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    913
    James Kanze
    Apr 28, 2008
  5. zvika
    Replies:
    2
    Views:
    128
    J├╝rgen Exner
    Dec 12, 2004
Loading...

Share This Page