String to Decimal

Hello everyone..... I had a question how I'm converting a string to decimal. It works fine but when I try to convert the string to a decimal number it reads it backwards.

eg. 000100

Instead of returning the a value of 4, it returns 8....

Any help to this issue is much appreciated.

Thanks.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

void Parsing(string s);
void Bin_to_Dec(string binary);

int main(){

    char sHex[8], again;

    do{
        cout << "Enter a 8 character hex word:" << endl;

        for (int n=0; n<8; n++){

            cin >> sHex[n];

           }

        string bReturn=" ", binChar;

        for (int i=0; i<=8; i++){

            bReturn = binChar + bReturn;

            char choice = sHex[i];

            choice = toupper(choice);

            switch (choice){

                case '0': binChar = "0000"; break;
                case '1': binChar = "0001"; break;
                case '2': binChar = "0010"; break;
                case '3': binChar = "0011"; break;
                case '4': binChar = "0100"; break;
                case '5': binChar = "0101"; break;
                case '6': binChar = "0110"; break;
                case '7': binChar = "0111"; break;
                case '8': binChar = "1000"; break;
                case '9': binChar = "1001"; break;
                case 'A': binChar = "1010"; break;
                case 'B': binChar = "1011"; break;
                case 'C': binChar = "1100"; break;
                case 'D': binChar = "1101"; break;
                case 'E': binChar = "1110"; break;
                case 'F': binChar = "1111"; break;
            }
        }

        cout << "\t" << bReturn << "\n" << endl;

        Parsing(bReturn);

        cout << "\a\nStart over? (y/n):"; cin >> again;
        cout << "\n";

    }while(again == 'y' || again == 'Y');

    return 0;
}

void Parsing(string s){

        string str = s;
        string op, rs, rt, rd, sh, funct;

            op = str.substr(0,5);
            rs = str.substr(6,10);
            rt = str.substr(11,15);
            rd = str.substr(16,20);
            sh = str.substr(21,25);
            funct = str.substr(26,31);

        cout << "op = " << op << endl;
        Bin_to_Dec(op);
        cout << "rs = " << rs << endl;
        Bin_to_Dec(rs);
        cout << "rt = " << rt << endl;
        Bin_to_Dec(rt);
        cout << "rd = " << rd << endl;
        Bin_to_Dec(rd);
        cout << "sh = " << sh << endl;
        Bin_to_Dec(sh);
        cout << "F(x) = " << funct << endl;
        Bin_to_Dec(funct);

}

void Bin_to_Dec(string binary){

        double decimal = 0;

        for(int c = 0; c < binary.size(); c++)
            if(binary.c_str()[c] == '1')
                decimal += pow(2, c);

        cout << "The decimal number is: " << decimal << "\n"<< endl;


}
Hello.

I didn't examine your code that deeply, so it cannot be helpful but I just write some minor concerns.

line 25: for (int i=0; i<=8; i++)
it reads to sHex[8]

line 99: no standard pow function takes integral values as first argument.
Last edited on
This code is unnecessarily complex:
1
2
3
for(int c = 0; c < binary.size(); c++)
            if(binary.c_str()[c] == '1')
                decimal += pow(2, c);


Use bit-wise operations instead. For example:
1
2
3
4
5
string binary = "100";
long value = 0;
for (char bit : binary) {
    value = (value << 1) | (bit == '1');
}
Topic archived. No new replies allowed.