×

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

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

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

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.