Convert Hexadecimal to Decimal Program in C++
Advertisements
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
Google Advertisment