text to binary

R

raghu

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
 
R

Richard Heathfield

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

raghu

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
 
O

osmium

raghu said:
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..
 
F

Fred Kleinschmidt

raghu said:
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>
 
K

Keith Thompson

raghu said:
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.
 
S

santosh

raghu said:
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.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

raghu said:
> 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 ?
 
R

raghu

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

jmcgill

/* 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 */
 
S

santosh

raghu said:
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.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

raghu said:
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');
}
}
 

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

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top