Hex to Decimal. Trouble with my Formulas

Trying to convert Hex to Decimal. Having some trouble. I think there is something wrong with my formula at the bottom of hex2dec.cpp or the for statement located there. Thanks in advance for any advice.







Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//
//  main.cpp
//  yost_shane_comp122_wk7_inclass_ex
//
//  Created by Shane Yost on 12/10/12.
//  Copyright (c) 2012 Shane Yost. All rights reserved.
//

#include <string>
#include <cstring>
#include <iostream>
using namespace std;
#include "hex2dec.h"
    
    int main()
    {
        char hex [10];
        int value = 0;
        cout << "Enter Hex value: ";
        cin >> hex;
        value = hex2dec(hex);
        cout << "The decimal conversion is " << value << endl;
        return 0;
    }






hex2dec.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
//  hex2dec.h
//  yost_shane_comp122_wk7_inclass_ex
//
//  Created by Shane Yost on 12/10/12.
//  Copyright (c) 2012 Shane Yost. All rights reserved.
//

#ifndef yost_shane_comp122_wk7_inclass_ex_hex2dec_h
#define yost_shane_comp122_wk7_inclass_ex_hex2dec_h



#endif

int hex2dec(char *hexValue);








hex2dec.cpp

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
//
//  File.cpp
//  yost_shane_comp122_wk7_inclass_ex
//
//  Created by Shane Yost on 12/10/12.
//  Copyright (c) 2012 Shane Yost. All rights reserved.
//

#include "hex2dec.h"
int hex2dec(char *hexValue)
{
    int dec [10], multiplier = 1, total = 0, i;
    for (int i = 0; i < 10; i++)
    {
    dec[i] = 0;
    }
    for (i = 0; i < 10; i++)
    {//Start of For
        switch (hexValue[i])
        {//Start of Switch
                
        case '0':
        dec[i] = 0;
        break;
                
        case '1':
        dec[i] = 1;
        break;
                
        case '2':
        dec[i] = 2;
        break;
                
        case '3':
        dec[i] = 3;
        break;
                
        case '4':
        dec[i] = 4;
        break;
                
        case '5':
        dec[i] = 5;
        break;
                
        case '6':
        dec[i] = 6;
        break;
                
        case '7':
        dec[i] = 7;
        break;
                
        case '8':
        dec[i] = 8;
        break;
                
        case '9':
        dec[i] = 9;
        break;
                
        case 'A':
        case 'a':
        dec[i] = 10;
        break;
                
        case 'B':
        case 'b':
        dec[i] = 11;
        break;
                
        case 'C':
        case 'c':
        dec[i] = 12;
        break;
                
        case 'D':
        case 'd':
        dec[i] = 13;
        break;
                
        case 'E':
        case 'e':
        dec[i] = 14;
        break;
                
        case 'F':
        case 'f':
        dec[i] = 15;
        break;
                
        }//End of Switch
    }//End of For
    
            for (--i; i >= 0; i++)
            {//Start of For
                total = total + dec[i] * multiplier;
                multiplier = multiplier * 16;
            }//Start of For
    return total;
}





Yes, the for loop at the end goes on forever, since i is increasing and will always be >= 0. Should be
for (--i; i>= 0; i--)

Apart from that, there is a problem here:

char hex [10];
int value = 0;
cout << "Enter Hex value: ";
cin >> hex;


hex is declared as an array of chars. However, while reading the input, you are reading only hex, meaning only 1 character. To make it as a string, you need to terminate it with a null character and then pass it, and in hex2dec function, you need to check the size of the string.

An easier approach is to use
string hex;
instead of
char hex [10];
. Then you can use cin>>hex directly.
Things you will need to change in that case:
1. Declaration of hex - string hex;
2. Function parameter in header file - int hex2dec(string hexValue);
Include the string header file here.
3. for loop in cpp file - for (i = 0; i < hexValue.size(); i++)

Hope this helps.
Topic archived. No new replies allowed.