Decimal to Hexadecimal in C
Decimal to Hexadecimal in C
Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a fractional part are separated by a decimal point. The dot in a decimal number is called a decimal point. However, this does not stand the same when it comes to programming language; decimal is a term that describes the base 10 number system. It is the most commonly used number system.
The decimal number system comprises single digits numbers ranging from 0 to 9, that is it ranges from 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The decimal number is also known as the base 10 number as it ranges from 0 to 9. Any combination of these ten digits (0 to 9) such as 33, 2453, 0, 8287894, and so on is called a decimal number.
A hexadecimal numbering system comprises of 16 symbols including ten digits from the standard numerical decimal system from 0 to 9 and 6 extra symbols from A to F that denotes the number 10 to 15. The hexadecimal numbering system is also known as the base 16 number system. There is no data type to store the hexadecimal values such as float or long, or double instead; you can store in the integral type of data types. The hexadecimal value is represented as 0x or 0X, and input hexadecimal value using scanf which has format specifiers like %x or %X.
E.g.:
Decimal input: 116 Hexadecimal output: 74 Decimal input: 10 Hexadecimal output: A Decimal input: 452 Hexadecimal output: 1C4
Algorithm:
- Divide the given decimal number by 16 and store the remainder in a temporary variable ‘temp’. If the temp < 10, do (48 + temp) in the character array; otherwise do (55 + temp) in the character array.
- Divide the number by 16.
- Repeat the step 1 and 2 until the quotient is zero.
- Print the array in reverse order.
E.g:
Consider a decimal number 443
- The remainder when 443 is divided by 16 is 11. Since the temp 11 > 10, do (55 + 11) = 66 = ‘B’.
- The quotient of from the step 1 is 27.
- The remainder when 27 is divided by 16 is 11. Since the temp 11 > 10, do ( 55 + 11) = 66 = ‘B’.
- The quotient from step 3 is 1.
- The remainder when 1 is divided by 16 is 1. Since the temp 1 < 10, do (1 + 48) = 49 = ‘1’.
- Quotient from step 5 will be 0. Once we get the quotient as 0, we stop iterating.
The obtained result is written in reverse order. For instance, in the above step we write the answer as ‘1BB’.
The reason behind adding the numbers 48 and 55 to the numbers that are less than 10 and more than 10 respectively is because of the ASCII encoding of characters in C.
When the remainder temp < 10 (less than), digit in hexadecimal ranges from 0 to 9. These character 0 to 9 are on the ASCII range of 48 to 57.
When the remainder temp > 10(more than) and always less than 15, digit in hexadecimal ranges from A to F. These character A to F are on the ASCII range of 65 to 70.
It is common to use string char[] digits = “0123456789ABCDEF” and use remainder as an index in the string.
Program to convert a decimal number into hexadecimal:
#include <stdio.h> #include <conio.h> #include <string.h> #include <math.h> int main() { long decimal, quotient, remainder; int i, j = 0; char hexadecimal [100]; printf (“Enter the decimal number to be converted: \n”); scanf (“%ld”, &decimal); quotient = decimal; while (quotient ! = 0) { remainder = quotient % 16; if (remainder < 10) hexadecimal [j++] = 48 + remainder; else hexadecimal [j++] = 55 + remainder; quotient = quotient / 16; } for (i = j; i >= 0; i--) printf (“The equivalent hexadecimal for the number %ld is: %c \n”, hexadecimal[i]); return 0; }
Output:
Enter the decimal number to be converted: 123 The equivalent hexadecimal for the number 123 is: 7B
- Input a decimal number and store it in a user defined variable ‘decimal’.
- Initialize a variable j to 0 and copy the value of ‘decimal’ to another variable, ‘quotient’.
- Obtain the quotient and the remainder variable ‘quotient’ and store the remainder in another variable ‘remainder’ and then override the variable quotient with the obtained quotient value.
- If the value of the remainder is less than 10, add 48 to the remainder and store the result in an array variable ‘hexadecimal’; else, add 55 to the remainder and store the obtained result to the same variable ‘hexadecimal’, which is a user defined variable.
- Repeat the above two steps until the value of the quotient becomes zero.
- Once the quotient is zero, print the array hexadecimal in the reversed way to get the output.