• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Binary to Int converter.

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

joshhua5

Member
Joined
Jan 29, 2010
http://pastebin.com/TZnZyfz6

the Conversion function, is going horribly wrong... and i have no idea.


Code:
#include <iostream>
#include <string>
using namespace std;
#include <stdlib.h>

string BINARY = "NULL";
string TempStr = "NULL";
int Bina = 0;
int y = 0;

int Conversion(string Bin)
{
	// Start Loop cycleing though the input
	for(int x = (Bin.length()-1); x >= 0; x--){
		
		TempStr = Bin[y];								// Put the current location being calculated into a string so it can be converted into a int


		//             16 8 4 2 1 
		// for example  1 1 0 0 1 which is 25
		// Using 1 * 2^4, 1 * 2^3, 0 * 2^2, 0 x 2^1, 1 * 2^0
		//           16 +   8    +    0   +    0   +    1    = 25
		/* for some reason the result being printed from cout below are:
		1  4   =  1 x 2^4          6       
		1  3   =  1 x 2^3          1
		0  2        "              2
		0  1        "              1
		1  0        "              2
		*/

		// I have the value calulated added to Bina each cycle.
		Bina += (atoi(TempStr.c_str())) * (2^x);		// converting the string from before into the integer for the 1 or 0, then * it to get it's value that it holds.
		cout << Bin[y] << " " << x << " " << ((atoi(TempStr.c_str()) * 2^x)) << "\n"; // Print out information for debugging purposes.
		//                                       ^^ This part is just 1 x 2^4   ect.
		y++; // Increase y by one so the cycle though the Binary works in order.
		

	}
	return Bina;
}


int main()
{
	while(true){
		// Reset data for loop
		TempStr = "0";
		y = 0;
		Bina = 0;
		// User input
		cin >> BINARY;
		// Being calculation
		cout << Conversion(BINARY) << "\n\n";
	}

}
 
you should look it up yourself, debug the program step by step.
I know where the problem is but it would be pointless to tell you.

for easy debugging change this:

Bina += (atoi(TempStr.c_str())) * (2^x);

to

int a = atoi(TempStr.c_str());
int b = 2^x;
int c = a * b;

this way you will be able to find the problem by checking all return values of those functions.

edit:
since TempStr is being used to store a single character you should define it as char.
and then it would look like atoi(&TempStr).
 
What about something like this?

Code:
int return_val = 0;
for(int i = 0; i < Bin.length(); i++){
   if (Bin[i] == '1'){ [I]// I'm not sure if the == operator is valid here, haven't worked with strings all that much... but you get the idea :p[/I]
      return_val |= 1 << i;
   }
}
return return_val;
 
What about something like this?

Code:
int return_val = 0;
for(int i = 0; i < Bin.length(); i++){
   if (Bin[i] == '1'){ [I]// I'm not sure if the == operator is valid here, haven't worked with strings all that much... but you get the idea :p[/I]
      return_val |= 1 << i;
   }
}
return return_val;
Because that's backwards. 0001 would come out of that as 8. You need to start with the zero power at the end of the string. Switch the initial value and ending conditions in your for loop to fix it (i = length-1; i >= 0).
 
Because that's backwards. 0001 would come out of that as 8. You need to start with the zero power at the end of the string. Switch the initial value and ending conditions in your for loop to fix it (i = length-1; i >= 0).

Oh I was actually trying to offer an alternate (perhaps more efficient) solution to the OP ... but yes! You have caught my embarrassing mistake! :chair:

Edit:
I thought about it a little more, switching the for loop around doesn't do anything. It should be

Code:
return_val |= 1 << (len(bin_string) - i - 1);

rather than

Code:
return_val |= 1 << i;

on line 4. :escape:
 
Last edited:
Back