Open In App

C Program For Hexadecimal to Decimal Conversion

Last Updated : 02 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Here we will build a C program for hexadecimal to decimal conversion using 5 different approaches i.e.

  1. Using format Specifier
  2. Using Switch case
  3. Using array 
  4. Using while loop
  5. Using for loop

We will keep the same input in all the mentioned approaches and get an output accordingly.

Input: 

hexanumber = "2D" 

Output: 

decimalnumber = 45

Method 1: Using format specifier

The format specifier %x represents the Hexadecimal number

C




// C program to demonstrate hexadecimal to decimal
// conversion using format specifier
  
#include <stdio.h>
  
int main()
{
  
    int n;
    // taking input from user
    scanf("%x", &n);
    
    // printing the decimal number
    printf("%d", n);
    return 0;
}


Output

Hexadecimal to decimal conversion output

Output

Method 2: Using switch case

C




// C program to demonstrate hexadecimal to decimal
// conversion using switch case
#include <math.h>
#include <stdio.h>
#include <string.h>
int main()
{
  
    char hexdecnumber[32] = "2D";
    int decimalnumber, i;
    
    // used to store the power index
    int cnt;
    
    // used to store the digit
    int digit;
  
    cnt = 0;
    decimalnumber = 0;
    
    // iterating the loop using length of hexadecnumber
    for (i = (strlen(hexdecnumber) - 1); i >= 0; i--) {
        
        // using switch case finding the equivalent decimal
        // digit for each hexa digit
        switch (hexdecnumber[i]) {
        case 'A':
            digit = 10;
            break;
        case 'B':
            digit = 11;
            break;
        case 'C':
            digit = 12;
            break;
        case 'D':
            digit = 13;
            break;
        case 'E':
            digit = 14;
            break;
        case 'F':
            digit = 15;
            break;
        default:
            digit = hexdecnumber[i] - 0x30;
        }
        
        // obtaining the decimal number
        decimalnumber = decimalnumber + (digit)*pow((double)16, (double)cnt);
        cnt++;
    }
    // printing the decimal number
    printf("Decimal number is: %d", decimalnumber);
    return 0;
}


Output

Decimal value is: 45

Method 3: Using Array

C




// C program to demonstrate hexadecimal to decimal
// conversion using an array
#include <math.h>
#include <stdio.h>
#include <string.h>
  
int main()
{
    int decimalNumber = 0;
    
    // Storing hexa number digits in one array
    char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
                           '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    char hexadecimalnumber[30] = "2D";
    int i, j, power = 0, digit;
  
    // Converting hexadecimal number to equivalent decimal
    // number
    for (i = strlen(hexadecimalnumber) - 1; i >= 0; i--) {
  
        // search if given input character is present in the
        // array or not. if it present in the array then
        // find the equivalent decimal number for each hexa
        // digit
        for (j = 0; j < 16; j++) {
            if (hexadecimalnumber[i] == hexDigits[j]) {
                decimalNumber += j * pow(16, power);
            }
        }
        power++;
    }
    // printing the result
    printf("Decimal Number : %d", decimalNumber);
  
    return 0;
}


Output

Decimal Number : 45

Method 4: Using while loop

C




// C program to demonstrate hexadecimal to decimal
// conversion using while loop
#include <math.h>
#include <stdio.h>
#include <string.h>
int main()
{
    char hexdecnumber[17] = "2D";
    int decimalnumber, place;
    int i = 0, val, len;
    decimalnumber = 0;
  
    // finding the length of hexadecnumber
    len = strlen(hexdecnumber);
    len--;
  
    // while loop executes the statements until the
    // condition is false
    while (hexdecnumber[i] != '\0') {
  
        // finding the equivalent decimal digit for each
        // hexa decimal digit
        if (hexdecnumber[i] >= '0'
            && hexdecnumber[i] <= '9') {
            val = hexdecnumber[i] - 48;
        }
        else if (hexdecnumber[i] >= 'a'
                 && hexdecnumber[i] <= 'f') {
            val = hexdecnumber[i] - 97 + 10;
        }
        else if (hexdecnumber[i] >= 'A'
                 && hexdecnumber[i] <= 'F') {
            val = hexdecnumber[i] - 65 + 10;
        }
        // final decimal number
        decimalnumber += val * pow(16, len);
        len--;
        i++;
    }
    // printing the result
    printf("Hexadecimal number = %s\n", hexdecnumber);
    printf("Decimal number = %d", decimalnumber);
    return 0;
}


Output

Hexadecimal number = 2D
Decimal number = 45

Method 5: Using for loop

C




// C program to demonstrate hexadecimal to decimal
// conversion using for loop
#include <math.h>
#include <stdio.h>
#include <string.h>
int main() {
  
    char hexdecnumber[17] = "2D";
    long long decimalnumber, place;
    int i = 0, val, len;
  
    decimalnumber = 0;
    place = 1;
  
    // Find the length of total number of hex digit
    // finding the length of hexa decimal number
    len = strlen(hexdecnumber);
    len--;
  
    // for loop iterates the hexa decimal number digits
    for (i = 0; hexdecnumber[i] != '\0'; i++) {
  
        // finding the equivalent decimal digit for each
        // hexa decimal digit
        if (hexdecnumber[i] >= '0'
            && hexdecnumber[i] <= '9') {
            val = hexdecnumber[i] - 48;
        }
        else if (hexdecnumber[i] >= 'a'
                 && hexdecnumber[i] <= 'f') {
            val = hexdecnumber[i] - 97 + 10;
        }
        else if (hexdecnumber[i] >= 'A'
                 && hexdecnumber[i] <= 'F') {
            val = hexdecnumber[i] - 65 + 10;
        }
  
        decimalnumber += val * pow(16, len);
        len--;
    }
    // printing the result
    printf("Hexadecimal number = %s\n", hexdecnumber);
    printf("Decimal number = %lld", decimalnumber);
    return 0;
}


Output

Hexadecimal number = 2D
Decimal number = 45


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads