Decimal to hexadecimal in C
In this post, you will learn how to convert decimal to hexadecimal using the C programming language.
A decimal number is a base-10 number system where the range of numbers is 0,1,2,3,4,5,6,7,8,9. The main advantages of the decimal number system are that it is easy-to-read, used by humans, and easy to manipulate. But there are some disadvantages, like wastage of space and time.
Hexadecimal describes a base-16 number system. The Hexadecimal Number System is generally used in computer programming and microprocessors. It is also helpful to describe the colours on web pages. The main advantage of a hexadecimal number is that it is exceptionally compact and, by utilising a base of 16, means that the number of digits used to address a given number is normally less than in binary or decimal.
Convert decimal to hexadecimal in C using Loop
For converting a decimal to hexadecimal, the standard mathematical approach is to divide the number by 16 until it reduces to zero. The sequence of remainders from last to first in a hex structure is the hexadecimal number system of the given decimal number. The following example demonstrates this-
#include <stdio.h>
int main() {
long int decimalnum, quotient;
int a = 1 , b , num;
char hexadecimal[ 100 ];
printf("Please enter decimal number: ");
scanf("%ld" , &decimalnum);
quotient = decimalnum ;
while( quotient != 0 ) {
num = quotient % 16 ;
if( num < 10 )
num = num + 48 ;
else
num = num + 55 ;
hexadecimal[a++]= num;
quotient = quotient / 16;
}
printf("Decimal to hexadecimal: ");
for(b = a -1 ; b > 0 ; b-- )
printf( "%c" , hexadecimal[ b ] );
return 0 ;
}
Output of the above code:
Please enter decimal number: 88
Decimal to hexadecimal: 58
Please enter decimal number: 66
Decimal to hexadecimal: 42
Convert decimal to hexadecimal in C using switch
In the given C program, we have used the switch statement to convert a decimal to a hexadecimal number.
#include <stdio.h>
int main()
{
int decimalnum, temp, x;
char hex [32];
printf("Please enter the decimal number: ") ;
scanf("%d", &decimalnum) ;
temp = 0 ;
while(decimalnum > 0)
{
switch(decimalnum % 16)
{
case 10 :
hex [temp] = 'A' ; break;
case 11 :
hex [temp] = 'B'; break;
case 12 :
hex [temp] = 'C' ; break;
case 13 :
hex [temp] = 'D'; break;
case 14 :
hex [temp] = 'E' ; break;
case 15 :
hex [temp] = 'F'; break;
default :
hex [temp] = (decimalnum%16) + 0x30;
}
decimalnum = decimalnum / 16;
temp++ ;
}
printf("Decimal to hexadecimal: ");
for( x = (temp-1) ; x >= 0 ; x--)
printf("%c" , hex[x]);
return 0;
}
Output of the above code:
Please enter the decimal number: 66
Decimal to hexadecimal: 42
Please enter the decimal number: 88
Decimal to hexadecimal: 58
Convert decimal to hexadecimal in C using the literal string
In the given C program, we have used the literal string to convert decimal to hexadecimal.
// C program to convert
// a decimal to hexadecimal
#include <stdio.h>
// function to convert decimal to hexadecimal
void decToHex(int decimalnum)
{
// declaration of character array
// to store hexadecimal number
char hexadecimal[50];
int i = 0;
while (decimalnum != 0)
{
hexadecimal[i] = (char)(*("0123456789ABCDEF"
+ ((decimalnum % 16) & (15))));
decimalnum = (decimalnum / 16);
++i;
}
// printing hexadecimal number array in reverse order
for (int j = i - 1; j >= 0; j--)
{
printf("%c", hexadecimal[j]);
}
}
int main()
{
int decimalnum;
printf("Enter a decimal number: ");
scanf("%d", &decimalnum);
printf("Decimal to hexadecimal: ");
decToHex(decimalnum);
return 0;
}
Output of the above code:
Enter a decimal number: 75
Decimal to hexadecimal: 4B
Enter a decimal number: 79
Decimal to hexadecimal: 4F
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative