Conversion from binary to decimal for data manipulation

I wrote an algorithim that reads from a .txt file. We have had some complications with software we are using and now have to read in from a .bin file.

My solution so I do not have to change my entire algorithim was to convert all the data from the .bin file into decimal, then proceed to manipulate the data.

My Code so far
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
54
55
56
57
58
59
60
61
62
63
64
#include <fstream>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int bin2dec(const char *bin, int x)
{
  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;
  x = result;
}


int main()
{
        char * memblock;
        char * memblock1;
        ifstream file ("c:\\testBin\\BEandDAexp4_11postINT.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];
           memblock1 = 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;
           
           ofstream myFile ("c:\\testBin\\BEandDAexp4_11postINT.bin", ios::out | ios::binary);
           myFile.seekp(ios::beg);
           myFile.write (memblock1, size);
           myFile.close();

           int y = 0;
           char * ted = memblock1;
           for( int i =0; i<100 ; ++i )
           {
               ted++;
               bin2dec(ted,y);                 // Helper Function from abover, converts bin to decimal, and sets y as the result
               cout<< "Info " << y <<endl;
           }
           file.close();
           
        }

        delete []memblock;
        delete []memblock1;
   


}


The problem is my out put is
1
2
3
4
5
6
Info 0
Info 0
Info 0
Info 0
Info 0
....


Can anyone figure out why I am not able to get the information?

Thanks

-Steave
I haven't really looked at the whole thing, but this is clearly wrong:

1
2
return result;
x = result;

The second line will never execute.

EDIT: you're not using the return value of bin2dec(). It doesn't even need two arguments. Notice that you initialize int y = 0; and then you just print it, without ever modifying its value.
Last edited on
You need to decide how you are going to return your result I think.

Either like this:

1
2
3
4
5
6
7
8
9
10
11
12
void bin2dec(const char *bin, int& x) // Note the & means the resuly will be passed back
{
  int result=0;
  for(;*bin;bin++)
  {
    if((*bin!='0')&&(*bin!='1'))
      return -1;
    result=result*2+(*bin-'0');
    if(result<=0) return -1;
  }
  x = result;
}


or this:


1
2
3
4
5
6
7
8
9
10
11
12
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;
}


And then use the function accordingly:

Either:

 
bin2dec(ted,y); 


or


 
y = bin2dec(ted); 

Also, when you want the function to set y as the result, pass it up by reference.
Line 44: You're writing trash to the file.
Line 48: You're passing trash to the converter function.
Line 49: What is the purpose of that loop?
Line 51: That's probably not doing what you want it to do.
Line 59: Move those delete [] statements into the space at Line 56.

1
2
3
4
5
6
7
8
9
10
11
#include <math.h>


void bin2dec(const char *bin, int &x, const int &size)
{
	float base = 2, j;
	for(int i=size-1; i>=0; --i) {     // consider vector<>::reverse_iterator
		j=pow(base,size-(i+1));
		x+=(int)(j*(bin[i]-'0'));
	}
}

Wow tons of help guys, way more than I expected Thank you very much

-Steave
Topic archived. No new replies allowed.