Hexadecimal to Decimal Conversion in C
Hexadecimal to Decimal Conversion
Here, in this section, we will discuss the C program for Hexadecimal to decimal conversion. A decimal number can be attained by multiplying every digit of binary digit with a power of 16 and adding each multiplication outcome. The power of the integer starts from 0 and counts to n-1 where n is assumed as the overall digits of integers in the HexaDecimal number.
Ex:- (1C6F)16 = 1 * 163 + 12 * 162 + 6 * 161 + 15 * 160
4096 + 3072 + 96 + 15 = (7279)10
How Does HexaDecimal Work?
Hexadecimal has numbers in the range of [0, 15]. With (0 – 9) represented as is and the others represented with alphabets.
- A – 10
- B – 11
- C – 12
- D – 13
- E – 14
- F – 15
Method 1
For a user input num. This requires you to know ASCII values, please check the ASCII table here
The method uses the following –
- Linear iterative loop spanning the character array
- Two sub-if-else loops to decide if the current character is a digit or alphabet
- Mathematical calculation to convert to decimal
Code in C
#include #include #include int convert(char hex[]) { int len = strlen(hex); int decimal = 0, pos = 0; for(int i = len - 1; i >= 0; i--) { // if given index value is a digit (0 - 9) if (hex[i] >= '0' && hex[i] <= '9') { // if character is in range '0' - '9' // can convert char value to its int value // by subtracting 48 (Refer Ascii table) as ASCII val 0 : 48 int digit = hex[i] - 48; decimal += digit * pow(16, pos); pos++; } // if given index is char in range [A, F] else if (hex[i] >= 'A' && hex[i] <= 'F') { // if character is in range 'A' - 'F' // can convert char value to its int value // by subtracting 55 (Refer Ascii table) as // ASCII val A : 65 and A must result 10 as value int digit = hex[i] - 55; decimal += digit * pow(16, pos); pos++; } } return decimal; } int main() { char hex[20]; scanf("%[^\n]", &hex); printf("%d",convert(hex)); return 0; }
Output
1C
28
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
For similar Questions click on given button.
- Highest Common Factor(HCF): C | C++ | Java | Python
- Lowest Common Multiple (LCM) : C | C++ | Java | Python
- Greatest Common Divisor : C | C++ | Java | Python
- Binary to Decimal to conversion : C | C++ | Java | Python
- Octal to Decimal conversion : C | C++ | Java | Python
- Hexadecimal to Decimal conversion: C | C++ | Java | Python
- Decimal to Binary conversion: C | C++ | Java | Python
- Decimal to Octal Conversion: C | C++ | Java | Python
- Decimal to Hexadecimal Conversion: C | C++ | Java | Python
- Binary to Octal conversion : C | C++ | Java | Python
- Octal to Binary conversion : C | C++ | Java | Python
- Quadrants in which a given coordinate lies : C | C++ | Java | Python
- Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
- Maximum number of handshakes: C | C++ | Java | Python
- Addition of two fractions: C | C++ | Java | Python
- Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
- Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
- Count possible decoding of a given digit sequence : C | C++ | Java | Python
- Calculate the area of a circle : C | C++ | Java | Python
- Find the prime numbers between 1 to 100 : C | C++ | Java | Python
- Calculate the number of digits in an integer : C | C++ | Java | Python
- Convert digit/number to words : C | C++ | Java | Python
- Counting number of days in a given month of a year: C | C++ | Java | Python
- Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
- Finding number of integers which has exactly x divisors: C | C++ | Java | Python
- Finding Roots of a quadratic equation : C | C++ | Java | Python
Hexadecimal to Decimal conversion the code that ur team provided is completely not up to the mark it is not executing the correct answer.check it once
Kindly join our Discord Channel for technical queries.