In this article, we are going to learn how to convert the Decimal to a Hexadecimal number. Here we are going to learn about two number systems one is Decimal and another is Hexadecimal. Decimal numbers are base 10 numbers and are represented in numbers from 0 to 9. Hexadecimal numbers are base 16 numbers and are represented in numbers from 0 to 9 and A to F.
C program to convert Decimal numbers to hexadecimal
#include <stdio.h>
char *strrevv(char *str);
char *strrevv(char *str)
{
char *ch1, *ch2;
if (! str || ! *str)
return str;
for (ch1 = str, ch2 = str + strlen(str) - 1; ch2 > ch1; ++ch1, --ch2)
{
*ch1 ^= *ch2;
*ch2 ^= *ch1;
*ch1 ^= *ch2;
}
return str;
}
int main()
{
char HEXVALUE[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
long long decimal, tempDecimal;
char hex[65];
int index, rem;
printf("Enter any decimal number: ");
scanf("%lld", &decimal);
tempDecimal = decimal;
index = 0;
while(tempDecimal !=0)
{
rem = tempDecimal % 16;
hex[index] = HEXVALUE[rem];
tempDecimal /= 16;
index++;
}
hex[index] = '
#include <stdio.h>
char *strrevv(char *str);
char *strrevv(char *str)
{
char *ch1, *ch2;
if (! str || ! *str)
return str;
for (ch1 = str, ch2 = str + strlen(str) - 1; ch2 > ch1; ++ch1, --ch2)
{
*ch1 ^= *ch2;
*ch2 ^= *ch1;
*ch1 ^= *ch2;
}
return str;
}
int main()
{
char HEXVALUE[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
long long decimal, tempDecimal;
char hex[65];
int index, rem;
printf("Enter any decimal number: ");
scanf("%lld", &decimal);
tempDecimal = decimal;
index = 0;
while(tempDecimal !=0)
{
rem = tempDecimal % 16;
hex[index] = HEXVALUE[rem];
tempDecimal /= 16;
index++;
}
hex[index] = '\0';
strrevv(hex);
printf("\nDecimal number = %lld\n", decimal);
printf("Hexadecimal number = %s", hex);
return 0;
}
';
strrevv(hex);
printf("\nDecimal number = %lld\n", decimal);
printf("Hexadecimal number = %s", hex);
return 0;
}
Output
Enter any decimal number: 47
Decimal number = 47
Hexadecimal number = 2F