Binary to Integer conversion continued

I am trying to read a .bin file and convert the results into either integers or doubles, write to a vector to run through an algorithim.

I ran the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        ifstream file ("c:\\testBin\\testBin.bin", ios::in|ios::binary|ios::ate);     // Opens file from indicated pathway
        int size;                                                                                   // ios:: ate sets the end of file operator  ios::binary ios::in, std fstream
        if(file.is_open()) 
        {
           cout<< "Attempting to Read Files" << endl;

           size = (int)file.tellg();                // Use's eof operator to see how large the file is.
           memblock = new char[size];          
           file.seekg(0,ios::beg);
           file.read(memblock, size);              // Reads file using 'size' as the itterator of where to stop, set by ios::ate

           for(int i =0; i<size; ++i)
           {
                cout<< static_cast<int>(memblock[i])) << endl;
           }

The output matched perfectly to what my binary reader out putted

1)Then I ran the code below, is there some reason you cannot type cast into a vector?
2) I am having a logical error with my bin2dec conversion, because my .bin file does not have the asci characters '0'&'1' might someone have a solution to that?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
int bin2dec(const char *bin) 
{
  int result=0;
  for(;*bin;bin++)
  {
    if((*bin!='0')&&(*bin!='1'))
      return -1;
    result=result*2+(*bin-'0');
    if(result<=0) return -1;
  }
  return result;
}




int main()
{
        char * memblock = 0;
        ifstream file ("c:\\testBin\\testBin.bin", ios::in|ios::binary|ios::ate);     // Opens file from indicated pathway
        int size;                                                                                   // ios:: ate sets the end of file operator  ios::binary ios::in, std fstream
        if(file.is_open()) 
        {
           cout<< "Attempting to Read Files" << endl;

           size = (int)file.tellg();                // Use's eof operator to see how large the file is.
           memblock = new char[size];          
           file.seekg(0,ios::beg);
           file.read(memblock, size);              // Reads file using 'size' as the itterator of where to stop, set by ios::ate
           cout<< "Files Read"<< endl;
           cout<< "Size " << size << endl;

           vector<char>values;
           for( int i =0; i<size ; ++i )
            {
                 values.push_back(static_cast<double>( memblock[i] ) );
            }

           cout<< values.size()<< endl;
           for( int i = 0; i<size; ++i)
           {
               cout<< values[i] << endl;
           }
           
            file.close();
            delete []memblock;
           
        }
   


}


With deep appreciation,

Steave
1)Then I ran the code below, is there some reason you cannot type cast into a vector?
1
2
3
4
5
6
           vector<char>values;
           for( int i =0; i<size ; ++i )
            {
                 values.push_back(static_cast<double>( memblock[i] ) );
            }


You can't insert a double into a vector of chars.


2) I am having a logical error with my bin2dec conversion, because my .bin file does not have the asci characters '0'&'1' might someone have a solution to that?

have you tried comparing the values against 0 and 1 rather than '0' and '1'. As you say, the file doesn't have the ascii characters representing 0 and 1, but rather they have 0 and one directly.

However, there might be other problems, because you are reading them one char at a time. a char is typically an 8 bit data type, and if your binary file is tightly packed, then you will have to add a loop of some kind similar to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

int bin2dec(const char *bin) 
{
  int result=0;
  for(;*bin;bin++)
  {
    int mask = 1;
    for(int i = 0; i < size(*bin), t++){
      result = result << 1;
      result = result + (*bin&mask);
      mast = mask << 1;
    }
  return result;
}

_<<_ is a bitwise shift operator
_&_ is a bitwise and.

I haven't tested it, so it may not be exactly right.
Last edited on
I got rid of the entire function and decided to try and cast the values instead do you think that this is a bad 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
27

int main()
{
        char * memblock = 0;
        ifstream file ("c:\\testBin\\testBin.bin", ios::in|ios::binary|ios::ate);     // Opens file from indicated pathway
        int size;                                                                                   // ios:: ate sets the end of file operator  ios::binary ios::in, std fstream
        if(file.is_open()) 
        {
           cout<< "Attempting to Read Files" << endl;

           size = (int)file.tellg();                // Use's eof operator to see how large the file is.
           memblock = new char[size];          
           file.seekg(0,ios::beg);
           file.read(memblock, size);              // Reads file using 'size' as the itterator of where to stop, set by ios::ate
           cout<< "Files Read"<< endl;
           cout<< "Size " << size << endl;

           vector<int>values;
           for( int i =0; i<size ; ++i )
            {
                 conv = static_cast<int>(memblock[i]);
                 values.push_back(conv);
            }
           
            file.close();
            delete []memblock;

Last edited on
Can you explain a bit better exactly what you are trying to do?

What is the input file format and what is the output file format that you want?
What I want out of this function is to output the values into a vector for furthur analysis.

The file format is simply repeated binary numbers .

Then I suspect that you don't need to be reading the file in as one block of characters and then converting. It would probably be much easier to simply read each integer in one at a time:

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
#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    std::ifstream file("datafile.bin", std::ios::binary);

    int i; // input integer

    // function read() returns the number of bytes read so when it returns
    // 0 (or less for error) the file is finished
    //
    //  reinterpret_cast takes the address of the integer and converts it to a char*
    //  because read() only reads characters. The sizeof(i) parameter tells read() how many
    //  chars to read for each int
    //
    while(file.read(reinterpret_cast<char*>(&i), sizeof(i)) > 0)
    {
        // ... do something with integer i here (add to vector?)
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.