468,457 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

text to binary

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

Oct 9 '06 #1
11 3836
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)
Oct 9 '06 #2
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)
Oct 9 '06 #3
"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><LFor <LF><CRand '\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..


Oct 9 '06 #4

"raghu" <ra*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>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
Oct 9 '06 #5
"raghu" <ra*********@gmail.comwrites:
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) ks***@mib.org <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.
Oct 9 '06 #6
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.

Oct 10 '06 #7
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 ?
Oct 10 '06 #8
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...

Oct 10 '06 #9
/* 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[i]);
printf("'%c' == 0x%02X == %sb\n", in[i], in[i], out);
}
return 0;
} /* main */
Oct 10 '06 #10
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.hyou 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.

Oct 10 '06 #11
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');
}
}
Oct 10 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Sami Viitanen | last post: by
17 posts views Thread by Guyon Morée | last post: by
27 posts views Thread by Eric | last post: by
3 posts views Thread by Tron Thomas | last post: by
8 posts views Thread by Yeow | last post: by
50 posts views Thread by Michael Mair | last post: by
12 posts views Thread by Adam J. Schaff | last post: by
10 posts views Thread by joelagnel | last post: by
4 posts views Thread by Florence | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.