×

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:

  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:

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

Related Topics

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.