?
Solved

converting a text to binary using basic C++ knowledge

Posted on 2003-03-27
13
Medium Priority
?
245 Views
Last Modified: 2013-11-15
And what I need is a program that will read in a text file and then convert that text into binary, its corresponding 1 and 0's. And then cout all of it on the screen. Doing all this with only using basic C++ knowledge, example using either a for loop or a while loop, not using any k statements, printf, scanf or szRet. Everything lower case, and if its possible to do it with out any recursion that would be great too but if not then ok. The program that I am using is microsoft visual c++ 6.0.  
0
Comment
Question by:stallion5000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +3
13 Comments
 
LVL 12

Expert Comment

by:Salte
ID: 8219632
errr...

I am a little confused here...

you want to open a text file and convert it to binary and then output the binary to screen?

I don't think the screen is too happy with binary output. For example the Win32 console will quickly start to display smileys and other funny characters if you try to output binary values. Especially if you only want to output 0 and 1. The 0 byte won't display anything and the 1 converts to a smiley or some such if I remember correctly.

What you possibly mean is to read the file as binary and then convert the binary codes to hexadecimal text values and then output those - the way a hex editor or hex viewer does. If this is what you want to do it is easy enough. However, a full program to do that is quite large and if you want me to write that for you I want some money. The core code of it is simple enough though:

unsigned char c;
ifstream file("foobar.bin",ios::binary|ios::in);

while (file.get(c)) {
   cout << ' ' << setfill('0') << setw(2) << int(c);
}

Now this code will just output a byte using 3 characters with 1 space and 2 hex digits in a long sequence. You probably want to break the line after a suitable number of characters (for example 16) and insert a newline at that point.

You might also want to display the ASCII of the same line and that is even easier, just pay attention to non-printable characters and characters that do funny things with your cursor and other attributes of your display and make sure that you don't output those unless you want them to.

The rest is just making it nice and tidy and perhaps handle various formats etc etc etc.

If you want the output in binary instead of hex that is also possible to do, but you will have to write the byte to binary conversion yourself, it isn't very difficult though, in fact it is extremely simple.

Alf
0
 
LVL 86

Expert Comment

by:jkr
ID: 8219711
If you want to output the bits, of each byte (if I understood you right), you could just use

void
__printbits (  char __b) {

unsigned char _mask, _count;

    for ( _count = 0, _mask = 0x80; _mask != 0; _mask = ( _mask >> 1)) {

        if ( __b & _mask) {

            printf ( "1");

        } else {

            printf ( "0");
        }
    }
}

void binarize () {

FILE* pf = fopen ( "file.bin", "rb");
char c;

 while ( !feof ( pf)) {

  c = fgetc ( pf); // get a byte
  __printbits ( c); // print the bits
 }

 fclose ( pf);
}
0
 

Author Comment

by:stallion5000
ID: 8219768
What I mean is a text file is read in and the text is converted to its ascii number and then after which those number is will be converted to binary.

Sorry for the confusion.

G
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 8219790
Whether text or binary, the above code will do that.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8219925
stallion,

that was what I assumed in my code :-)

Alf
0
 

Expert Comment

by:VINO
ID: 8220034
I personally think the best answer of them was jkr one, but they both will work, it depends on wethere you want to use iostream.h or stdlib.h. i think for your aclaration you don't understand what they did, let me explain that for you.
To get a text file into its ascii values you simply have to open it as binary to read. It will directly read 1 byte at a time (1 char at a time), the only thing you have to take care about is that de '\n' text caracter is converted into 2 binary chars '\n' '\r'. then you can simply print this byte to the screen with printf(), or the cout<<.
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8222456
> What I mean is a text file is read in and the text is converted to its ascii number and then after which those number is will be converted to binary.

Still confusing.
Can you just give some examples like table below ?

FILE     INTERNAL     SCREEN
....     .....        ....
0
 

Expert Comment

by:VINO
ID: 8224420
file:
hello world\n (in text mode)
internal reading (ascii in decimal value):
104 101 108 108 111 32 119 111 114 108 100 11 13
(they're entered one at a time by the fscanf or getc functions)

then with the mask function that jkr did you'll see:
01101000 01100101 01101100 01101100 01101111 00100000
01110111 01101111 01110010 01101100 01100100 00001011
00001101

i don't know if this will work for you, it's just what it's supposed to be (and of course what it is). remember, the only key here is that opening a text file in binary automaticly reads the ascii code of each char.

I don't want to confuse you more, so if the next thing does you just simply forget about it.
if you open the file as a text file, the character you get by getc, if you copy it to a short integer and you see it, i think, i'm not 100 % sure (more likely 80%), you'll get the ascii code, you may try doing this as well....
0
 
LVL 12

Expert Comment

by:Salte
ID: 8224656
VINO,

Your explanation seems a bit odd...

It's not that binary file causes a newline to translate to \r \n sequence (CR LF). The thing is that text files translates \n to \r \n on output (in windows) and translates \r \n to \n on input while binary files do not so they read the raw \r \n (if that is what is in the file).

Binary files means no translation is done from the system. Text files means that some systems (unix does not) translate a system defined 'end of line sequence' to \n when reading the file and translates a \n to the 'end of line sequence' when writing the file.

There are even systems that doesn't do any such translation at all but instead store text files in a different manner and not indicating the end of line by a separate character at all. Some OS store text files with a 'length of the line' data one place and then the characters of the file are simply stored without any end of line sequence at all. In such a case a \n will simply cause the system to mark this line as 'done', store the line's length and start writing a new line without actually outputting any character into the file. Similarly, such systems will then read the line until the end and then give you a \n char and at the same time move the position to the start of the next line on reading. That \n wasn't in the file at all.

When opening a binary file the idea is that you don't want any system specific structure on the file and you just want to read the raw data from the file.

Alf
0
 
LVL 2

Accepted Solution

by:
Francoz earned 2000 total points
ID: 8239933
Suppose you got char by char reading of the file. Then call convert to binary

ConvertToBinary(char c)
{
 unsigned char ch = c;
 unsigned char mask = UCHAR_MAX - ( UCHAR_MAX >> 1 ) ;
 static char result [CHAR_BIT + 1] ;
 char *store = result ;
     
 while ( mask )
 {
         
 *store++ = ( char ) ( '0' + ( ( ch & mask) != 0 ) ) ;
 mask >>= 1 ;
 }
     
 *store = '\0' ;
 return result ;
}

Thre return value will be the string pointer with 1's and 0's filled which is equivalent to the input character.

Does this func help you?
0
 

Author Comment

by:stallion5000
ID: 8240503
Thanks a bunch. Short and sweet.

G
0
 
LVL 12

Expert Comment

by:Salte
ID: 8244026
Just to make it clear, the answer you accepted is not an answer that you requested. It is possible it is the answer you wanted but it wasn't what you asked for.

Alf
0
 
LVL 12

Expert Comment

by:Salte
ID: 8244278
stallion,

I suggest you do NOT open the file as a text file. If you want a binary dump of the file you should open the file as a binary file. Opening a file as text file means that you get a translation of bytes so what you see in the binary dump is  not necessarily what the file contains.

Alf
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question