How To Convert Decimal to Hexadecimal In C

Number systems enable the processing, storing and accessing of computer instructions as well as data. Various types of number systems supported by the computer system such as decimal, binary, octal and hexadecimal. Decimal number is a user-interpretable number that starts from 0 and ends with 9 i.e. (0-9) and its base is 10. Another hexadecimal number range starts from 0 to  9 i.e. (0-9) and A for 10, B for 11…..F for 15 i.e. (A-F) for (10-15). Hexadecimal number base is 16. Notation for the decimal number and hexadecimal number representation is given below table:

Number SystemSyntaxExample
Decimal(number)10  (524)10
Hexadecimal(number)10  (A2B4)16

Why Is There A Need To Convert Decimal to Hexadecimal  In C

For the computer instruction processing and addressing, we need to convert decimal numbers into hexadecimal numbers.

  • Address representation: Hexadecimal number is used to represent various addresses of the computer system such as memory address, ethernet address or MAC address.
  • Color code representation: The color code is also represented by the two digits hexadecimal code.
  • To represent an error message: Hexadecimal number presents memory location of programming errors. It helps programmers to find and debug errors.

Approaches to Convert Decimal to Hexadecimal

To convert a decimal number to hexadecimal, several approaches are available. There are some approaches listed below. 

  • Approach-1: Using Arithmetic Operator i.e. Modulus (%) Operator
  • Approach-2: Using Format Specifier i.e. %X
  • Approach-3: Using Division (/) and Multiplication (*) Operator
  • Approach-4: Using User Defined Function

Lets dive in details for each approach.

Approach-1: Using Arithmetic Operator i.e Modulus (%) Operator

In C, an arithmetic operator i.e. modulus (%) allows the conversion of decimal numbers into hexadecimal code. In this approach, the decimal number is divided with 16 and the remainder stored in the array until the quotient becomes zero i.e. 0. At the end array printed in reverse order. An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Divide decimal number using modulus operator(%) until quotient is 0.

Step-3: Store each remainder in a character array.

Step-4: Display array in reverse order.

Code:

// Demonstration of conversion of Decimal to Hexadecimal using the  arithmetic operator i.e. modulus(%) 
#include <stdio.h>       // include header file
int main()               // main block is begin
{
  
    int DeciNum;        // Declaration of Decimal number
    int i = 1, j, NumRemainder;
    char HexNum[200];                      // Declaration of Hexadecimal number
    printf("Enter decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DeciNum);                      // It store number in to DeciNum 
    while (DeciNum != 0) {          // Check DeciNum is not equal to 0 
        NumRemainder = DeciNum % 16;      // Divide DeciNum by 16 using modulus operator 
        if (NumRemainder < 10)
            NumRemainder = NumRemainder + 48;
        else
            NumRemainder = NumRemainder + 55;
        HexNum[i++] = NumRemainder;
        DeciNum = DeciNum / 16;
    }
    printf("Hexadecimal value is: ");   // It print hexadecimal number
    for (j = i - 1; j > 0; j--)
        printf("%c",HexNum[j]);
    return 0;
}

Output:

Enter Decimal Number: 126
Hexadecimal Number is: 7E

 Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask decimal number from the user
  • Divide decimal number with 16 and store remainder in character array using modulus operator.
  • Check remainder is less than 10 then it computes ascii code for 0-9 number otherwise computes ascii value for A-Z characters.

Approach-2: Using Format Specifier i.e. %X

Another approach is format specifier i.e. %X which may be used to convert decimal numbers to hexadecimal code. This method is accomplished via using %X format specifier in printf function with respect to the given decimal number. An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Use format specifier i.e. %X in printf() function.

Step-3: Display hexadecimal code for given decimal number.

Code:

// Demonstration of conversion of Decimal to Hexadecimal using format specifier i.e. %X
#include <stdio.h>       // include header file
int main()               // main block is begin
{  
  int DeciNum;        // Declaration of Decimal number
   printf("Enter decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DeciNum);             // It store number into DeciNum 
    printf("Hexadecimal value is: %X",DeciNum);   // It print hexadecimal number
    return 0;
}

Output:

Enter decimal number: 255
Hexadecimal value is: FF

Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask decimal number from the user
  • Use format specifier %X in printf() function during print the value of Decimal number i.e. DeciNum in Hexadecimal representation.

Approach-3: Using  Division(/) and Multiplication Operator(*)

Without use of modulus operator (%), we can also convert decimal to hexadecimal using division (/) and multiplication (*) operators. This approach is defined in the following algorithm.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Divide decimal number using division operator(/)  and multiply qoutient with 16 using multiplication operator.

Step-3: Remainder is computed by the difference of Decimal number and (quotient *16). 

Step-4 Store each remainder in a character array.

Step-5: Repeat step 2, 3,4 until DeciNum is 0

Step-6: Display array in reverse order.

Code:

// Demonstration of conversion of Decimal to Hexadecimal using the  division and multiplication operator
#include <stdio.h>       // include header file
int main()               // main block is begin
{
  
    int DeciNum;        // Declaration of Decimal number
    int ch,i = 1, j,NR, NumRemainder;
    char HexNum[200];               // Declaration of Hexadecimal number
    printf("Enter decimal number: ");    // It asks decimal number from the user
    scanf("%d",&DeciNum);             // It store number into DeciNum 
    while (DeciNum != 0) {          // Check DeciNum is not equal to 0 
             
        ch = DeciNum / 16;  // Divide DeciNum by 16 using division (/) operator 
        NR = ch * 16;
        NumRemainder = DeciNum - NR;
		if (NumRemainder < 10)
            NumRemainder = NumRemainder + 48;
        else
            NumRemainder = NumRemainder + 55; 
		HexNum[i++] = NumRemainder;
        DeciNum = DeciNum / 16;
    }
    printf("Hexadecimal value is: ");   // It print hexadecimal number
    for (j = i - 1; j > 0; j--)
        printf("%c",HexNum[j]);
    return 0;
}

Output:

Enter decimal number: 345
Hexadecimal value is: 159

Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask a decimal number from the user.
  • Divide decimal number with 16 and compute remainder in character array using division and multiplication operators. 
  • Check remainder is less than 10 then it computes ascii code for 0-9 number otherwise computes ascii value for A-Z characters.

Approach-4: Using User Defined Function

With the help of user defined function, a decimal number may also be converted into hexadecimal code. In this approach, a function is created which takes a decimal number and returns hexadecimal code. An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Create a user defined function named Decimal_to_Hex(DeciNum) which takes a decimal number as an argument. 

Step-3: Decimal_to_Hex(DeciNum) function converts a decimal number to hexadecimal using format specifier.

Step-4: Decimal_to_Hex(DeciNum) returns hexadecimal code.

Code:

// Demonstration of conversion of Decimal to Hexadecimal using User Defined Function
#include <stdio.h>       // include header file
int Decimal_to_Hex(int);  // Here Decimal_to_Hex() Function prototype is defined
int main()               // main block is begin
{
    int DeciNum;        // Declaration of Decimal number
    int HexNum;                      // Declaration of Hexadecimal number
    printf("Enter decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DeciNum);                      // It store number in to DeciNum 
    Decimal_to_Hex(DeciNum);      // Decimal_to_Hex function is called    
    return 0;
}
int Decimal_to_Hex(int DeciNum)
{
 printf("Hexadecimal value is: %X",DeciNum); // It print hexadecimal number	
 return 0;
}

Output:

Enter decimal number: 512
Hexadecimal value is: 200

Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask a decimal number from the user.
  • Function Decimal_to_Hex() is created which takes DeciNum as an argument. 
  • Decimal_to_Hex() converts a decimal number to hexadecimal using format specifier.
  • Decimal_to_Hex() displays hexadecimal code. 

Best Approach- Format Specifier i.e. %X

The best approach  to convert  a decimal number to hexadecimal code is format specifier i.e. %X approach. It is the best approach because:

  • It is  a short and very convenient way to decimal to hexadecimal code.
  • It may be used in any in-built function such as printf(), scanf() and user defined functions.     
  • It offers a short length of code.

Sample problems related to convert decimal to hexadecimal

Sample Problem-1: Using Approach-1

Problem Definition: Consider a computer system that processes a set of instructions supplied by the user in decimal form. Therefore, it is required to convert decimal to hexadecimal for the given input for computer processing.  Create an algorithm and  C code to demonstrate how to convert a decimal number to hexadecimal.

Solution: An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take an instruction for the computer in decimal number from the user.

Step-2: Divide decimal number using modulus operator(%) until quotient is 0.

Step-3: Store each remainder in a character array.

Step-4: Display array in reverse order.

Code:

// Demonstration of conversion of Decimal to Hexadecimal using approach-1
#include <stdio.h>       // include header file
int main()               // main block is begin
{ 
    int DeciNum;        // Declaration of Decimal number
    int i = 1, j, NumRemainder;
    char HexNum[200];               // Declaration of Hexadecimal number
    printf("Enter computer instruction in decimal form: ");      // It asks a instruction in decimal number from the user
    scanf("%d",&DeciNum);                 // It store number into DeciNum 
    while (DeciNum != 0) {          // Check DeciNum is not equal to 0 
        NumRemainder = DeciNum % 16;      // Divide DeciNum by 16 using modulus operator 
        if (NumRemainder < 10)
            NumRemainder = NumRemainder + 48;
        else
            NumRemainder = NumRemainder + 55;
        HexNum[i++] = NumRemainder;
        DeciNum = DeciNum / 16;
    }
    printf("Computer instruction in Hexadecimal form is: ");   // It print hexadecimal number
    for (j = i - 1; j > 0; j--)
        printf("%c",HexNum[j]);
    return 0;
}

Output:

Enter computer instruction in decimal form: 42
Computer instruction in Hexadecimal form is: 2A

  Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask computer instruction in decimal numbers from the user.
  • Divide decimal number with 16 and store remainder in character array using modulus operator.
  • Check remainder is less than 10 then it computes ascii code for 0-9 number otherwise computes ascii value for A-Z characters.

Sample Problem-2:Using Approach-2

Problem Definition: Consider a  company that wants to develop a digital calculator whose motive is to implement digital number system conversion. Digital calculator is able to convert one number format to another. For example to convert a decimal to binary, octal and hexadecimal as well as vice versa.  Therefore create an algorithm and  C code to demonstrate how to convert a decimal number to hexadecimal.

Solution: An algorithm for this problem is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Use format specifier i.e. %X in printf() function.

Step-3: Display hexadecimal code for given decimal number.

Code:

// Demonstration of conversion of Decimal to Hexadecimal using approach-2
#include <stdio.h>       // include header file
int main()               // main block is begin
{
  
    int DeciNum;        // Declaration of Decimal number
    printf("Enter decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DeciNum);                      // It store number in to DeciNum 
    printf("Hexadecimal value is: %X",DeciNum);   // It print hexadecimal number
    return 0;
}

Output:

Enter decimal number: 780
Hexadecimal value is: 30C

 Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask decimal number from the user
  • Use format specifier %X in printf() function during print the value of Decimal number i.e. DeciNum in Hexadecimal representation.

Sample Problem-3: Using Approach-3

Problem Definition: Consider a programmer who wants to format web pages background and paragraph font color using hexadecimal code. But the color code is given in decimal numbers. Therefore, it is required to convert decimal numbers into hexadecimal code. Though, create an algorithm and  C code to demonstrate the conversions.

Solution: An algorithm for this problem is represented in the following steps.

Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Divide decimal number using division operator(/)  and multiply qoutient with 16 using multiplication operator.

Step-3: Remainder is computed by the difference of Decimal number and (quotient *16). 

Step-4 Store each remainder in a character array.

Step-5: Repeat step 2, 3,4 until DeciNum is 0

Step-6: Display array in reverse order.

Code:

// Demonstration of conversion of Decimal to Hexadecimal using approach-3
#include <stdio.h>       // include header file
int main()               // main block is begin
{
  
    int DeciNum;        // Declaration of Decimal number
    int ch,i = 1, j,NR, NumRemainder;
    char HexNum[200];                      // Declaration of Hexadecimal number
    printf("Enter decimal number: ");      // It asks decimal number from the user
    scanf("%d",&DeciNum);                      // It store number in to DeciNum 
    while (DeciNum != 0) {          // Check DeciNum is not equal to 0 
             
        ch = DeciNum / 16;  // Divide DeciNum by 16 using division (/) operator 
        NR = ch * 16;
        NumRemainder = DeciNum - NR;
		if (NumRemainder < 10)
            NumRemainder = NumRemainder + 48;
        else
            NumRemainder = NumRemainder + 55; 
		HexNum[i++] = NumRemainder;
        DeciNum = DeciNum / 16;
    }
    printf("Hexadecimal value is: ");   // It print hexadecimal number
    for (j = i - 1; j > 0; j--)
        printf("%c",HexNum[j]);
    return 0;
}

Output:

Enter color code in decimal number: 16711680
Color code in Hexadecimal value is: FF0000

 Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask a decimal number from the user.
  • Divide decimal number with 16 and compute remainder in character array using division and multiplication operators. 
  • Check remainder is less than 10 then it computes ascii code for 0-9 number otherwise computes ascii value for A-Z characters.

 Sample Problem-4: Using Approach-4

Problem Definition: Consider a network system physical address represented by the hexadecimal code. A physical address of the system is asked from the user in decimal number. Therefore, create an algorithm and  C code to demonstrate how to convert network physical addresses from decimal to hexadecimal form. 

Solution: An algorithm for this problem is represented in the following steps.

 Algorithm: 

Step-1: Take a decimal number from the user.

Step-2: Create a user defined function named Decimal_to_Hex(DeciNum) which takes a decimal number as an argument. 

Step-3: Decimal_to_Hex(DeciNum) function converts a decimal number to hexadecimal using format specifier.

Step-4: Decimal_to_Hex(DeciNum) returns hexadecimal code.

Code:

// Demonstration of conversion of Decimal to Hexadecimal using approach-4
#include <stdio.h>       // include header file
int Decimal_to_Hex(int);  // Here Decimal_to_Hex() Function prototype is defined
int main()               // main block is begin
{
    int DeciNum;        // Declaration of Decimal number
    int HexNum;                      // Declaration of Hexadecimal number
    printf("Enter physical address in decimal form: ");      // It asks decimal number from the user
    scanf("%d",&DeciNum);                      // It store number in to DeciNum 
    Decimal_to_Hex(DeciNum);      // Decimal_to_Hex function is called    
    return 0;
}
int Decimal_to_Hex(int DeciNum)
{
 printf("Hexadecimal value for network physical address is: %X",DeciNum); // It print hexadecimal number	
 return 0;
}

Output:

Enter physical address in decimal form: 43467889
Hexadecimal value for network physical address is: 2974471

Code Explanation:

  • Include header file i.e. stdio.h.
  • Ask a network physical address in decimal number from the user.
  • Function Decimal_to_Hex() is created which takes DeciNum as an argument. 
  • Decimal_to_Hex() converts a decimal number to hexadecimal using format specifier.
  • Decimal_to_Hex() displays hexadecimal code for the given address. 

Conclusion

Number system is an important component for the computer system which supports processing, storing and accessing of the data. A decimal number may be converted into different other numbers such as binary, octal, hexadecimal and vice versa. Here, many approaches for converting a decimal number to hexadecimal code were presented. A format specifier approach is considered as a suitable approach due to the shortened and convenient way. In this, all approaches for the conversion of a decimal number to hexadecimal code have been presented with an algorithm, C program, output and its explanation.