Reading from a binary file, outputting 0s and 1s as txt

Hello!

Basically, I have a program that captures data from the serial port of the computer and saves it as a binary file whose output if opened in notepad is something like this:
ÿbýýýbýýýbýýýbýýýbýýýbýýýbýýý"ýýýbýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýý"ýýýbýýýbýýýbýý....

Now, I want to be able to open this file and actually read the bits sequentially. there are important patterns that I need to determine for a project that I am working on. Now, I know that a binary file could be open as someone posted the code in these forums, which I've edited to my purposes as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;

int main(){
ifstream fin;
fin.open("LogicAnalyzer.txt",ifstream::binary|ifstream::ate);
int size;
size=(int) fin.tellg();
fin.seekg (0, ifstream::beg);
ofstream fout;
fout.open("output.txt");
char bindata[size];
while(!fin.eof()){    
   fin.read(bindata,size);
}   
fout<<bindata;
return 0;
}


Now, the problem is that the data that I've just read and out put in the file output.txt is the exact same. I can't understand it. What I require is an output files either containing 0's and 1's or HEX representation. I need this and can't do it through a HEX converter/viewer etc. because I have to search for patterns and stuff like that. Any help will be highly appreciated. Thanks!
I didn't understand the issue here. Isn't each char comprised of 8 bits. So the array of chars that you have read already has the bits, all you need to do is to scan each char bit by bit.
What I require is an output files either containing 0's and 1's or HEX representation.
Do you mean character representation? For Hex you can use the hex format of the stream: http://www.cplusplus.com/reference/iostream/ios_base/setf/

first thing, dont put binary data in char. use unsigned char.
Hello!
What I mean is that whatever data that I store in the char bindata, I want to be able to output in a text file as a character or integer sequence if one's and zero's
closed account (z05DSL3A)
an idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
{
    ifstream fin;
    fin.open("c:\\test.txt");
    
    unsigned char bindata;
    stringstream oss;

    while(fin.good())
    {    
        bindata = fin.get();
        if(fin.good())
        {
            for(int i = 0; i < 8; ++i)
            {
                unsigned char mask = 0x80;
                mask = mask >> i;
                oss << ((bindata & mask)? '1' : '0');
            }
        }
    } 

    std::cout << oss.str();
    
return 0;
}
@Grey Wolf...that does not work....any other ideas?
@coder777....does not work...the stuff that I read in the variable bindata, when I out put it as:
1
2
3
cout.setf ( ios::hex, ios::basefield );       // set hex as the basefield
cout.setf ( ios::showbase );                  // activate showbase
cout<<bindata; 


I get garbage rather than HEX
It must be:
1
2
3
4
5
6
cout.setf ( ios::hex, ios::basefield );       // set hex as the basefield
cout.setf ( ios::showbase );                  // activate showbase
for(int i = 0; i < size; ++)
{
  cout<<bindata[i]; // << ' '?
}
Ther might be an issue with signed/unsigned though. You also may want a space between the values.

What's the problem with Grey Wolf's solution?
it will be garbage if you see them in text editor.. did you see that in any hex editor like notepad++ or ultraedit?
Ok, I should have checked it. This works:

1
2
3
4
5
6
cout.setf ( ios::hex, ios::basefield );       // set hex as the basefield
cout.setf ( ios::showbase );                  // activate showbase
for(int i = 0; i < size; ++i)
{
  cout<<(unsigned int) bindata[i] << ' '; // ?
}


EDIT: I checked Grey Wolf's algorithm also and it works fine
Last edited on
closed account (z05DSL3A)
champion7891 wrote:
@Grey Wolf...that does not work....any other ideas?

I admit that it is probably not the most elegant solution but in principle it works fine, you just have to tailer it to your needs.
Topic archived. No new replies allowed.