Hexadecimal to Decimal in C++
In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15, with 10 representing A, 11 representing B, 12 representing C, 13 representing D, 14 representing E, and 15 representing F.
Let us look at an example of a program in C++ converting hexadecimal to decimal:
// C++ program to convert hexadecimal to decimal
#include < bits/stdc++.h >
#include < stdlib >
#include < iostream >
using namespace std ;
// Function to convert hexadecimal to decimal
int hexadecimalToDecimalnum ( string hexVal )
{
int length = hexValue.size ( ) ;
// Initializing base value to 1, i.e 16 ^ 0
int base = 1 ;
int dec_value = 0 ;
// extracting characters as digits from last
// character
for ( int i = len - 1 ; i >= 0 ; i-- ) {
// if character lies in ' 0 ' - ' 9 ', converting
// it to integral 0 - 9 by subtracting 48 from
// ASCII value
if ( hexValue [ i ] >= '0' && hexValue [ i ] <= '9' ) {
dec_value += ( int ( hexValue [ i ] ) - 48 ) * base ;
// incrementing base by power
base = base * 16 ;
}
// if character lies in 'A' - 'F' , converting
// it to integral 10 - 15 by subtracting 55
// from ASCII value
else if ( hexValue [ i ] >= 'A' && hexValue [ i ] <= 'F' ) {
dec_value += ( int ( hexValue [ i ] ) - 55 ) * base ;
// incrementing base by power
base = base * 16 ;
}
}
return dec_value ;
}
// driver program
int main ( )
{
string hexNumber = "CAA" ;
cout << ( hexadecimalToDecimalnum ( hexNumber ) ) ;
return 0 ;
}
OUTPUT:
3242
……..
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
The goal is to maintain a dynamic dec_value and subtract the digits of the total hexadecimal number starting from the most right digit. Multiply the digit with the correct base (16 Power) and add it to the variable dec_value when subtracting hexadecimal number digits. Finally, the decimal number will be saved in the variable dec_value.
Another Example:
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
#include < string >
using namespace std ;
//convert hexadecimal to decimal
int convert ( char num [ ] ) {
int len = strlen ( num ) ;
int base = 1 ;
int temp = 0 ;
for ( int i = len - 1 ; i >= 0 ; i-- ) {
if ( num [ i ] >= '0' && num [ i ] <= '9' ) {
temp += ( num [ i ] - 48 ) * base ;
base = base * 16 ;
}
else if ( num [ i ] >= 'A' && num [ i ] <= 'F' ) {
temp += ( num [ i ] - 55 ) * base ;
base = base * 16 ;
}
}
return temp ;
}
int main ( ) {
char num [ ] = "CAA" ;
cout << num << " after converting to decimal becomes : " << convert ( num ) <<endl ;
return 0 ;
}
OUTPUT:
CAA after converting to decimal becomes: 3242
………………………………………………………………………
Process executed in 0.12 seconds
Press any key to continue.
Explanation:
In the above program in C++, we will extract digits from right to left through a remainder, then multiply it with a power starting at 0 and increasing by 1 until the (number of digits) – 1 value is reached. Because we'll be converting from hexdecimal to binary, the power base will be 16, as octal has a base of 16. Multiply the digits of the provided inputs by base and power then save the results. To get the final result, which will be a decimal number, add all the multiplied values together.
Let us look at an example of a program using pre defined function in C++ converting hexadecimal to decimal:
// C++ program to convert octal to decimal
#include < bits/stdc++.h >
#include < iostream >
#include < stdlib >
using namespace std ;
int HexToDecimal ( string n ) {
return stoi ( n , 0 , 16 ) ;
}
int main ( )
{
string n = "CAA" ;
cout << HexToDecimal ( n ) ;
return 0 ;
}
OUTPUT:
3242
……..
Process executed in 0.11 seconds
Press any key to continue.
Explanation:
In the above program in C++, we are using HexToDecimal function which we are passing string n and returning the stoi to get our hexadecimal to decimal number.