×

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

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

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

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.