codescracker


c

C Program to Convert Decimal to Hexadecimal



« Previous Program Next Program »

In this tutorial, we will learn about how to create a program in C that converts any given decimal number (by user at run-time) into its equivalent hexadecimal value. We have also created a user-defined function-driven program that does the same job of converting decimal number to hexadecimal number.

Decimal to Hexadecimal Conversion in C

Decimal number has its base as 10. That means it has total of 10 digits that are 0, 1, 2, ..., 8, 9. And Hexadecimal number has its base as 16. That means it has total of 16 digits that are 0, 1, 2, ..., 7, 8, 9, 10 (represented by A), 11 (represented by B), 12 (represented by C), 13 (represented by D), 14 (represented by E), 15 (represented by F). Because 10 has two digit that are 0 and 1, in same way 11 has two digit that are 1 and 1 and so on. Therefore 10 is repsented with A, in same way 11 is represented with B, ..., 15 is represented with F.

To convert decimal number to hexadecimal number in C programming, you have to ask from user to enter the decimal number to convert it into hexadecimal number, and then display the equivalent value in hexadecimal as output.

C Programming Code to Convert Decimal to Hexadecimal

Following C program asks from user to enter any number (in decimal) to convert it into hexadecimal, then display the result on the screen:

// Write a program in C that converts Decimal to Hexadecimal
// --------codescracker.com--------

#include<stdio.h>
#include<conio.h>
int main()
{
    int decnum, rem, i=0;
    char hexnum[50];
    printf("Enter any decimal number: ");
    scanf("%d", &decnum);
    while(decnum!=0)
    {
        rem = decnum%16;
        if(rem<10)
            rem = rem+48;
        else
            rem = rem+55;
        hexnum[i] = rem;
        i++;
        decnum = decnum/16;
    }
    printf("\nEquivalent Value in Hexadecimal = ");
    for(i=i-1; i>=0; i--)
        printf("%c", hexnum[i]);
    getch();
    return 0;
}

As the above program was build and run under Code::Blocks IDE, therefore after successful build and run, you will get the following output. This is the first snapshot of the sample run:

c decimal to hexadecimal

Now supply any decimal number as input say 172 and press ENTER key to see the equivalent hexadecimal value as output. Here is the second snapshot of the sample run:

c program convert decimal to hexadecimal

Here is another sample run. This is the final snapshot of the sample run:

decimal to hexadecimal in c

Here are some of the main steps used in above program:

C Decimal to Hexadecimal without Modulous Operator

Now let's create the same program but without using any modulous operator. That is we have to find out the remainder without using modulous operator. Therefore to find out remainder, we have divided the number present inside decnum by 16, and stored its quotient value inside a variable say temp, then again multiply the quotient value with 16 and stored the multiplication result to a variable say chck. Now subtract chck from decnum (decnum - chck) and initialized it to rem variable, that will holds the current remainder.

For example, if user has supplied 172 as input, therefore at first run decnum/16 or 10 gets initialized to temp Then temp*16 or 10*16 or 160 gets initialized to chck. Now decnum-chck or 172-160 or 12 gets initialized to rem which is the remainder this time. In this way, we have replaced the modulous operator with three line of code as shown in the program given below:

// Write a program in C that converts any decimal number 
// (entered by user at run-time) to hexadecimal number
// ---------codescracker.com---------

#include<stdio.h>
#include<conio.h>
int main()
{
    int decnum, hexnum[50], temp, chck, i=0, rem;
    printf("Enter any Decimal number: ");
    scanf("%d", &decnum);
    while(decnum!=0)
    {
        temp = decnum/16;
        chck = temp*16;
        rem = decnum - chck;
        if(rem<10)
            rem = rem+48;
        else
            rem = rem+55;
        hexnum[i] = rem;
        i++;
        decnum = temp;
    }
    printf("\nEquivalent Hexadecimal Value = ");
    for(i=i-1; i>=0; i--)
        printf("%c", hexnum[i]);
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c decimal to hexadecimal without modulous

C Decimal to Hexadecimal using User-defined Function

Now let's create a user-defined function named DecToHex() that takes one argument (the decimal number entered by user). Here we have declared the variable i and the array hex[] outside both the function say main() and DecToHex() to make it known for both the function. Here the variable i is declared as static variable because static variable remembers its previous value. One last thing is, we have not initialized the static variable i with 0 (which is required), because by default static variable holds 0 as its initial value after declaring the static variable. Rest of the thing is similar that can easily be understand by you.

// Write a program in C that converts Decimal to Hexadecimal number
// using user-defined function
// --------codescracker.com--------

#include<stdio.h>
#include<conio.h>
void DecToHex(int dec);
static int i;
char hex[50];
int main()
{
    int decnum;
    printf("Enter any decimal number: ");
    scanf("%d", &decnum);
    DecToHex(decnum);
    printf("\nEquivalent Value in Hexadecimal = ");
    for(i=i-1; i>=0; i--)
        printf("%c", hex[i]);
    getch();
    return 0;
}
void DecToHex(int dec)
{
    int rem;
    while(dec!=0)
    {
        rem = dec%16;
        if(rem<10)
            rem = rem+48;
        else
            rem = rem+55;
        hex[i] = rem;
        i++;
        dec = dec/16;
    }
}

Below is the final snapshot of the sample run of above program:

c decimal to hexadecimal using function

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »






Tools
Calculator

Quick Links
Signup - Login - Give Online Test