0

i'm trying to make a code can convert an image to binary(o-1) and it worked well but there are many symbols (ASCII characters) missed in the result of my code while when i open the image in hex editor i find the full image converted and i find these characters converted too , and when i tried to make a counter beside every binary comes out , i noticed the counter stops before the missed character and starts again after passing it. here is my code..

#include <string>
#include <bitset>
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
   ifstream file("E:\\2.jpg");
   string myString;
   ofstream fout("E:\\mnmn.txt");
   while(file>>myString)
   {
   for (size_t i = 0; i < myString.size(); ++i)  
 {
  fout <<i <<"-"<< bitset<8>(myString.c_str()[i])<<endl;
 }}
return 0;

}

the result comes out like:

0-11111111
1-11011000
2-11111111
3-11100000
4-00000000
5-00010000
0-01001010    \\passed 00001001 character and started the counter from the beginig
1-00000000
etc...

Thank you in advance for anyone would help.

2 Answers2

0

Try opening the istream with ios::binary:

ifstream file("E:\\2.jpg", ios::binary);

If this is not present data will be interpreted as text and some bytes which may represent control characters may be discarded.

Additionally try using istream::read() to read the binary file. This member function doesn't try to interpret control characters. This could look something like this (untested):

file.seekg(std::ios_base::end);
size_t length = file.tellg();
file.seekg(std::ios_base::beg);
myString.reserve(length);
file.read(&myString[0], length);
BenMorel
  • 34,448
  • 50
  • 182
  • 322
UsYer
  • 173
  • 1
  • 8
0

You are trying to read std::string from a binary file (and not opening the file as ios::binary). [Binary file = "not text"]

You should really use stream::read to read data from a binary file. The std::string and operator >> aren't really meant to read raw binary data, and will certainly not work well for this purpose.

The reason it stops reading and skips over the byte 00001001 is that it is a "whitespace character" - tab in this particular case. You can stop it skipping whitespace by file >> noskipws >> something;, but it doesn't help the fact that it will, always, stop reading at newline. Since you can't guarantee that a jpg file doesn't contain a byte for newline, it's impossible to process JPG files using operator>> for strings.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Would you please tell me how can i do that ??? .. how can i convert jpg or video to zero and one without skips ? – user3487485 Apr 06 '14 at 13:06
  • If you don't care much about efficiency (and you are writing to console output, so that's going to dwarf any other thing), you could just loop around `while(file.get(c)) ...` where `c` is a `char` - most of your existing code would remain, except you are reading a byte at a time. I hope you have a fairly small JPG, because large file would make a very long output... – Mats Petersson Apr 06 '14 at 13:12