In this Answer, we will convert a decimal number (numbers having the base 10) to hexadecimal numbers (numbers having the base 16) in C language.
For example, we have a decimal number 28, and we want to convert it to its hexadecimal equivalent, 1C; there are several approaches we can take in C language to do so, but we will be discussing two of them.
This approach uses the modulus division operator to convert the decimal number to hexadecimal. We take the modulus with 16 and keep on storing it in a character array after either adding it with 48 or 55 for hexadecimal character conversion. This decision is based on whether or not the result of the modulus operation is greater than or equal to 10, as the hexadecimal numbers greater than 9, ranging from 10–15 are represented by A
, B
, C
, D
, E
, and F
, respectively. This continues until the decimal number's division by 16 reaches 0.
Following is the code for this approach:
#include <stdio.h>int main(){// taking the decimal number inputint n;scanf("%d", &n);// printing the decimal numberprintf("Decimal number: %d\n", n);// x is the index of hex_array[]// z stores the modulus value in each iterationint x = 1, z;char hex_array[100];// while the decimal number n is not equal to zero upon divisionwhile (n != 0){// storing the modulus with 16 in the variable zz = n % 16;// converting to hex equivalent based on the value of zif (z >= 10){z = z + 55;}else {z = z + 48;}// storing the hexadecimal equivalent in hex_array[]hex_array[x++] = z;// dividing the decimal number n by 16n = n / 16;}// printing hex_array[] in hexadecimal number formatprintf("The number in hexadecimal is: ");for (int y = x - 1; y > 0; y--){printf("%c", hex_array[y]);}return 0;}
Enter the input below
In the code provided above, we perform the following steps:
Lines 5–10: We read and print the input value n
, a decimal number provided by the user, to be converted.
Lines 14–15: We create a char
array to store the hexadecimal equivalent of the decimal number where the variable x
keeps track of the array's index in all iterations and z
is an integer used to store the modulus value of n
.
Lines 20–32: We take the modulus of the decimal number n
with 16
and store it in the variable z
.
To convert the number to its hexadecimal equivalent, we add z
with 55
if z >= 10
to convert into its char
form since numbers greater than 9
in hexadecimal are represented by A
, B
, C
, D
, E
and F
.
Otherwise, the number z
is added with 48
to store the char
equivalent of the range 0-9
in the array, hex_array[]
.
Line 35: We divide the number n
by 16
to enter the next iteration of conversion to check whether or not it is greater than 0
.
Lines 39–43: We print the char
array hex_array[]
to display the hexadecimal output.
In this approach, we use the simple built-in format specifier in C for hexadecimal numbers. It is indicated by %X
.Through
Here is the code for this approach:
#include <stdio.h>int main(){// taking the decimal number inputint n;scanf("%d", &n);// printing the decimal numberprintf("Decimal number: %d\n", n);// using %X formal specifier to print hexadecimal outputprintf("The number in hexadecimal is: %X", n);return 0;}
Enter the input below
From lines 5–10, we take and print the input decimal number n
, and in line 13, we use the format specifier %X
to convert the decimal number n
to hexadecimal directly.
Free Resources