Link to home
Start Free TrialLog in
Avatar of stallion5000
stallion5000

asked on

converting a text to binary using basic C++ knowledge

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.  
Avatar of Salte
Salte

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
Avatar of jkr
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);
}
Avatar of stallion5000

ASKER

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
Whether text or binary, the above code will do that.
stallion,

that was what I assumed in my code :-)

Alf
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<<.
> 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
....     .....        ....
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....
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
ASKER CERTIFIED SOLUTION
Avatar of Francoz
Francoz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a bunch. Short and sweet.

G
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
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