C++ Case Study: Converting Hexadecimals to Decimals

This section presents a program that converts a hexadecimal number into a decimal number.

Section 5.8.4, “Case Study: Converting Decimals to Hexadecimals,” gives a program that converts a decimal to a hexadecimal. How do you convert a hex number into a decimal?

Given a hexadecimal number hnhn-1hn-2 … h2h1h0, the equivalent decimal value is

hn x 16n   +    hn -1 x   16n 1 +   hn2  x   16n2 + … +    h2 x   162 + h1  x   161 + h0  x   160

For example, the hex number AB8C is

10 X 163 + 11 X 162 + 8 X 161 + 12 X 160 = 43916

Our program will prompt the user to enter a hex number as a string and convert it into a decimal using the following function:

int hex2Dec(const string& hex)

A brute-force approach is to convert each hex character into a decimal number, multiply it by 16′ for a hex digit at the i ’s position, and then add all the items together to obtain the equivalent decimal value for the hex number.

Note that

hn X 16n + hn – 1 X 16n-1 + hn-2 X 16n-2 + … + h1 X 161 + h0 X 160

= (… ((hn X 16 + hn – 1) X 16 + hn– 2) X 16 + … + h1) X 16 + h0

This observation, known as the Horner’s algorithm, leads to the following efficient code for converting a hex string to a decimal number:

int decimalValue = 0;

for (int i = 0; i < hex.size(); i++)

{

char hexChar = hex[i];

decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);

}

Here is a trace of the algorithm for hex number AB8C:

Listing 6.18 gives the complete program.

Listing 6.18 Hex2Dec.cpp

1 #include <iostream>
2
#include <string>
3
#include <cctype>
4
using namespace std;
5
6
// Converts a hex number as a string to decimal
7 int hex2Dec(const string& hex);

8
9
// Converts a hex character to a decimal value
10 int hexCharToDecimal(char ch);
11
12
int main()
13 {
14   
// Prompt the user to enter a hex number as a string
15    cout << “Enter a hex number: “;
16    string hex;
17    cin >> hex;
18
19    cout <<
“The decimal value for hex number ” << hex
20    <<
” is ” << hex2Dec(hex) << endl;
21
22   
return 0;
23 }
24
25
int hex2Dec(const string& hex)
26 {
27   
int decimalValue = 0;
28   
for (unsigned i = 0; i < hex.size(); i++)
29    decimalValue = decimalValue *
16 + hexCharToDecimal(hex[i]);
30
31   
return decimalValue;
32 }
33
34
int hexCharToDecimal(char ch)
35 {
36    ch = toupper(ch);
// Change it to uppercase
37    if (ch >= ‘A’ && ch <= ‘F’)
38   
return 10 + ch – ‘A’;
39   
else // ch is ‘0’, ‘1’, …, or ‘9’
40    return ch – ‘0’;
41 }

The program reads a string from the console (line 17), and invokes the hex2Dec function to convert a hex string to decimal number (line 20).

The hex2Dec function is defined in lines 25–32 to return an integer. The string parameter is declared as a const and passed by reference, because the string is not changed in the function and it saves memory by passing it as reference. The length of the string is determined by invoking hex.size() in line 28.

The hexCharToDecimal function is defined in lines 34–41 to return a decimal value for a hex character. The character can be in either lowercase or uppercase. It is converted to uppercase in line 36. Recall that to subtract two characters is to subtract their ASCII codes. For example, ‘5’ – ‘0’ is 5.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *