Convert Hexadecimal to Decimal Program in C++

Google Advertisements

Prev Tutorial Next Tutorial

C++ program to Convert Hexadecimal to Decimal

To convert Hexadecimal number to Decimal number in C++ programming, We have to ask to the user to enter the hexadecimal number to convert it into Decimal number to display the equivalent value in binary format on screen.

Example

0=0000
1=0001
2=0010
3=0011
4=0100
5=0101
6=0110
7=0111
8=1000
9=1001
A=10
B=11
C=12
D=13
E=14
F=15

Example

Input Hexadecimal Number : 2E
Euivalent Decimal Number : 46

Input Hexadecimal Number : 1B
Euivalent Decimal Number : 27

Input Hexadecimal Number : CC
Euivalent Decimal Number : 204

C++ program to convert Hexadecimal to Decimal

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

void main ()
{
    char num[20];
    int i, r, len, hex = 0;
    clrscr();
    cout<<"Enter any Hexadecimal Number : ";
    cin>>num;
    len = strlen(num);
    for (i = 0; num[i] != '\0'; i++)
    {
        len--;
        if(num[i] >= '0' && num[i] <= '9')
            r = num[i] - 48;
        else if(num[i] >= 'a' && num[i] <= 'f')
                r = num[i] - 87;
             else if(num[i] >= 'A' && num[i] <= 'F')
                    r = num[i] - 55;
        hex += r * pow(16,len);
    }
    cout<<"\nEquivalent Decimal Value " << num << " is : " << hex;
    getch();
}

Output

Enter any Hexadecimal Number: 2B
Equivalent Decimal Value is: 43
c++ program to convert hexadecimal to decimal
Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. For Advertisement contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs