I have a binary string that I am trying to convert into a decimal. In order to do that I have to access each number (0, 1) in the string. The num1 string = "00001" after the sub-string
Here's my code:
1 2 3 4 5 6 7 8
string num1 = b.substr(0, 5); // Grabbing the first 5 bits of the 32-bit string
num1[1] = atoi(num1[1].c_str()); // Want to get each index of the string converted to int..this does not work
// Decode each sub-string
double dec1 = ((num1[0] * 0) + (num1[1] * pow(2.0, 3)) + (num1[2] * pow(2.0, 2))
+ (num1[3] * pow(2.0, 1)) + (num1[4] * pow(2.0, 0))); // Binary to Decimal
cout << dec1 << " " << num1[1] << " " << ((num1[1]) * (pow(2.0, 3.0))) << " " << (0 * (pow(2.0, 3.0))) << endl;
The only problem with that is each 5 bits needs to be separate from the others. The last instruction will be 7 bits long, with the last 2 being fillers. So I need to convert each bit to from string (zero or one) to int (zero or one).
If I'm not mistaken, there should be custom numeric classes to handle things like this. There was a contest not to long ago on the forums containing BIG NUMs.