Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement