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.:

Algorithm:

1. 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.
2. Divide the number by 16.
3. Repeat the step 1 and 2 until the quotient is zero.
4. Print the array in reverse order.

E.g:

Consider a decimal number 443

1. The remainder when 443 is divided by 16 is 11. Since the temp 11 > 10, do (55 + 11) = 66 = ‘B’.
2. The quotient of from the step 1 is 27.
3. The remainder when 27 is divided by 16 is 11. Since the temp 11 > 10, do ( 55 + 11) = 66 = ‘B’.
4. The quotient from step 3 is 1.
5. The remainder when 1 is divided by 16 is 1. Since the temp 1 < 10, do (1 + 48) = 49 = ‘1’.
6. 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:

Output:

• 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.