473,699 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4479
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*********@gm ail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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*********@gm ail.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_Keit h) 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
39401
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
10486
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
4996
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 any solid reasons to prefer text files over binary files files?
3
3485
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 possible to change the behavior of operator << so it will stream numeric values as their actual values when an ofstream is in binary mode. I did not, however, find any information on how this can be accomplished. What is involved in getting this...
8
9524
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 contains the following three floating-point numbers: 1.0 2.0 3.0
50
4949
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
36
3590
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
5886
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: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
10
3653
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 binary mode. according to me, notepad opens files and each byte of the file read, it converts that byte from ascii to its correct character and displays
4
9545
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 code that processes all text files in a directory but leaves binary files alone. -- http://www.florencesoft.com
0
8686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9033
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7748
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6533
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2345
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.