C program to convert Decimal to Hexadecimal

In this blog post, we will see the C program to convert Decimal to Hexadecimal. In the interview, people ask the below questions,

  • How will you convert decimal to hexadecimal value?
  • Write a C program to convert decimal number system value to hexadecimal number system?
  • Implement logic to convert a decimal number to a hexadecimal number system?
  • Get a decimal number from the user and convert it to its hexadecimal equivalent?
  • How do you convert the number with base value 10 to base value 16?

 

Before writing the code let’s understand what hexadecimal and decimal are.

The hexadecimal number system is a base 16 number system. The hexadecimal number is represented by 16 values i.e 0 1 2 3 4 5 6 7 8 9 A B C D E F.

The decimal number system is a base 10 number system. It uses 10 symbols to represent all numbers i.e. 0123456789

 

Conversion steps from decimal to hex:

  • Divide the number by 16.
  • Get the integer quotient for the next iteration.
  • Get the remainder for the hex digit.
  • Repeat the steps until the quotient is equal to 0.

Example,

Convert 671110 to hex:

Division
by 16
Quotient
(integer)
Remainder
(decimal)
Remainder
(hex)
Digit #
6711/16 419 7 7 0
419/16 26 3 3 1
26/16 1 10 A 2
1/16 0 1 1 3

 

Decimal to hexadecimal conversion table:

The following table shows 0-16 decimal numbers with the corresponding hexadecimal numbers.

Decimalbase 10 Hexbase 16
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 A
11 B
12 C
13 D
14 E
15 F
16 10

 

C Program to convert a decimal number to a hexadecimal number:

The following are C programs to convert a decimal number to a hexadecimal number.

Method 1:

// C program to convert a decimal
//

#include <stdio.h>

// function to convert decimal to hexadecimal
void decToHex(int decNum)
{
    // char array to store hexadecimal number
    char hexaDeciNum[50];

    // counter for hexadecimal number array
    int i = 0;
    while (decNum != 0)
    {
        /* temporary variable to
        store right most digit*/
        int temp = 0;

        // Get the right most digit
        temp = decNum % 16;

        // check if temp < 10
        if (temp < 10)
        {
            hexaDeciNum[i] = temp + 48;
            i++;
        }
        else
        {
            hexaDeciNum[i] = temp + 55;
            i++;
        }

        decNum = decNum / 16; // get the quotient
    }

    printf("0x"); //print hex symbol

    // printing hexadecimal number array in reverse order
    for (int j = i - 1; j >= 0; j--)
    {
        printf("%c", hexaDeciNum[j]);
    }
}

int main()
{
    int decNum;
    
    printf("Enter a decimal number: ");
    scanf("%d", &decNum);
    
    decToHex(decNum);

    return 0;
}

Output:

Decimal to hexadecimal conversion table aticleworld

 

How it works:

Step 1: Calculate remainder when 6711 is divided by 16 is 7. Therefore, temp = 7. As temp is less than 10. So, hexaDeciNum[0] = 48 + 7= 55 = ‘7’.

Step 2: Divide 6711 by 16. New number is 6711/16 = 419.

Step 3: Calculate remainder when 419 is divided by 16 is 3. Therefore, temp = 3. As temp is less than 10. So, hexaDeciNum[0] = 48 + 3= 51= ‘3’.

Step 4: Divide 419 by 16. New number is 419 /16 = 26.

Step 5: Calculate remainder when 26 is divided by 16 is 10. Therefore, temp = 10. As temp is greater than 10. So, hexaDeciNum[0] = 55 + 10= 65= ‘A’.

Step 6: Divide 26 by 16. New number is 26/16 = 1.

Step 7: Calculate remainder when 1 is divided by 16 is 1. Therefore, temp = 1. As temp is less than 10. So, hexaDeciNum[0] = 48 + 1= 49 = ‘1’.

Step 8: Divide 1 by 16. New number is 1/16 = 10.

Step 9: Now number becomes = 0, so iterating loops will break and print hexaDeciNum array reverse order.

 

Method-2:

The steps are the almost same as the above method. Here I have used the ternary operator in place of the if-else statement.

#include <stdio.h>

//function to convert decimal to hexadecimal
void decToHex(int decNum)
{
    // char array to store hexadecimal number
    char hexaDeciNum[50];

    // counter for hexadecimal number array
    int i = 0;

    while (decNum != 0)
    {
        /* temporary variable to
        store right most digit*/
        int temp = decNum % 16;

        // check if temp < 10
        hexaDeciNum[i++] = temp + ((temp < 10) ? 48 :55);

        decNum = decNum / 16; // get the quotient
    }
    printf("0x"); //print hex symbol

    // printing hexadecimal number array in reverse order
    for (int j = i - 1; j >= 0; j--)
    {
        printf("%c", hexaDeciNum[j]);
    }
}
int main()
{
    int decNum;

    printf("Enter a decimal number: ");
    scanf("%d", &decNum);

    decToHex(decNum);
    
    return 0;
}

 

Method-3:

Using the literal string you can also convert decimal to hex.

// C program to convert a decimal

#include <stdio.h>

// function to convert decimal to hexadecimal
void decToHex(int decNum)
{
    // char array to store hexadecimal number
    char hexaDeciNum[50];
    int i = 0;

    while (decNum != 0)
    {
        hexaDeciNum[i] = (char)(*("0123456789ABCDEF"
                                  + ((decNum % 16) & (15))));
        decNum = (decNum / 16);
        ++i;
    }

    printf("0x"); //print hex symbol

    // printing hexadecimal number array in reverse order
    for (int j = i - 1; j >= 0; j--)
    {
        printf("%c", hexaDeciNum[j]);
    }
}

int main()
{
    int decNum;

    printf("Enter a decimal number: ");
    scanf("%d", &decNum);

    decToHex(decNum);

    return 0;
}

 

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *