Convert a decimal number to hexadecimal in C without Modulus
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.
Approach 1: Through modulus division
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.
Code
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
Explanation
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
chararray to store the hexadecimal equivalent of the decimal number where the variablexkeeps track of the array's index in all iterations andzis an integer used to store the modulus value ofn.Lines 20–32: We take the modulus of the decimal number
nwith16and store it in the variablez.To convert the number to its hexadecimal equivalent, we add
zwith55ifz >= 10to convert into itscharform since numbers greater than9in hexadecimal are represented byA,B,C,D,EandF.Otherwise, the number
zis added with48to store thecharequivalent of the range0-9in the array,hex_array[].
Line 35: We divide the number
nby16to enter the next iteration of conversion to check whether or not it is greater than0.Lines 39–43: We print the
chararrayhex_array[]to display the hexadecimal output.
Approach 2: Through format specifier
In this approach, we use the simple built-in format specifier in C for hexadecimal numbers. It is indicated by %X.Through
Code
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
Explanation
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