How to convert Hexadecimal to Decimal in C++

In this article, we are going to learn How to convert Hexadecimal to Decimal in C++. Here we are going to learn about two number systems one is Decimal and another is Hexadecimal. Hexadecimal numbers are base 16 numbers and represented in numbers from 0 to 9 and A to F.Decimal numbers are base 10 numbers and represented in numbers from 0 to 9.

How to convert Hexadecimal to Decimal


In this C++ program to convert Hexadecimal to Decimal, we have custom code without using any built-in function

#include<iostream>
#include <math.h>
#include <string.h>

using namespace std;

int main()
{
    char hexNum[17];
    long long decimalNum, place;
    int val, len;

    decimalNum = 0;
    place = 1;

    cout << "Please enter a Hexadecimal Number: ";
    cin >> hexNum;

    len = strlen(hexNum);
    len--;

    for(int i=0; hexNum[i] != '
#include<iostream>
#include <math.h>
#include <string.h>
using namespace std;
int main()
{
char hexNum[17];
long long decimalNum, place;
int val, len;
decimalNum = 0;
place = 1;
cout << "Please enter a Hexadecimal Number: ";
cin >> hexNum;
len = strlen(hexNum);
len--;
for(int i=0; hexNum[i] != '\0'; i++)
{
if(hexNum[i]>='0' && hexNum[i]<='9')
{
val = hexNum[i] - 48;
}
else if(hexNum[i]>='a' && hexNum[i]<='f')
{
val = hexNum[i] - 97 + 10;
}
else if(hexNum[i]>='A' && hexNum[i]<='F')
{
val = hexNum[i] - 65 + 10;
}
decimalNum += val * pow(16, len);
len--;
}
cout << "\nThe Decimal equivalent of HexaDecimal number " <<  hexNum << " is " << decimalNum;
return 0;
}
'; i++) { if(hexNum[i]>='0' && hexNum[i]<='9') { val = hexNum[i] - 48; } else if(hexNum[i]>='a' && hexNum[i]<='f') { val = hexNum[i] - 97 + 10; } else if(hexNum[i]>='A' && hexNum[i]<='F') { val = hexNum[i] - 65 + 10; } decimalNum += val * pow(16, len); len--; } cout << "\nThe Decimal equivalent of HexaDecimal number " << hexNum << " is " << decimalNum; return 0; }