473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 4450
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Sami Viitanen | last post by:
Hello, How can I check if a file is binary or text? There was some easy way but I forgot it.. Thanks in adv.
17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
36
by: Wei Su | last post by:
Hi, I have a text file abc.txt and it looks like: 12 34 56 23 45 56 33 56 78 ... .. .. ... .. .. I want to get how many rows totally in the text file, how to do this? Thanks.
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
4
by: Florence | last post by:
How can a binary file be distinguished from a text file on Windows? Obviously I want a way that is more sophisicated that just looking at the dot extention in the filename. I want to write...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.