Introduction
There are mathematical calculations that must occasionally be made in programming, and conversion from decimal to hexadecimal in C++ simple and straightforward.
Let's first discuss decimal and hexadecimal numbers.
Decimal Number: The most widely used and accepted system in daily life is the decimal numeral system. It bases itself on the number 10. (radix). Consequently, it has ten symbols: The digits 0 through 9, specifically 0, 1, 2, 3, 4, 5, 6, 7, and 9.
Many ancient civilizations used the decimal numeral system, which is one of the earliest numeral systems that is currently known. The Hindu-Arabic numeral system solved the problem of representing very large numbers in the decimal system. Using powers of base 10, the Hindu-Arabic numeral system assigns positions to each digit in a number. Each digit is then raised to the nth power according to its position.
Hexadecimal Number: The base of the hexadecimal system (abbreviated hex) is 16. (radix). It uses 16 symbols because it is a base-16 numeral system. The first six letters of the English alphabet are A, B, C, D, E, and F, and the first ten decimal digits are 0 through 9 (0, 1, 2, 3, 5, 6, 7, and 8). The need to represent the numbers 10, 11, 12, 13, 14, and 15 in a single symbol necessitates the use of the letters.
In mathematics and information technology, hex is used to represent binary numbers because it is more aesthetically pleasing. Hex is a language used to write binary in an abbreviated form because each hex digit represents four binary digits.
A half-byte (also known as a nibble) is made up of four binary digits. Accordingly, a single byte can store binary values between 0000 0000 and 1111 1111. These can be more amiably represented in hex, where they range from 00 to FF.
In HTML programming, colours are denoted by a 6-digit hexadecimal number. For example, white is denoted by FFFFFF, while black is denoted by 000000.
We use following table to covert decimal to hexadecimal in c++.
Hexadecimal Digit | Decimal Digit |
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
A | 10 |
B | 11 |
C | 12 |
D | 13 |
E | 14 |
F | 15 |
Problem Statement
Write a program to convert decimal to hexadecimal in c++. i.e. convert a given decimal number into an equivalent hexadecimal number from input of a decimal number is given.
Sample Example
Input 1:
Decimal number = 5386
Output 1:
Heaxadecimal = 150A
Input 2:
Decimal number = 2545
Output 2:
Hexadecimal = 9F1
Also Read - C++ Interview Questions and Application of Oops
Naive Approach
This method, which involves dividing the number to be converted, is simple. Since the hexadecimal number system's base number is 16, if N is a decimal number, divide it by 16. Write down the remainder's value, which will range from 0 to 15. (replace 10, 11, 12, 13, 14, 15 by A, B, C, D, E, F respectively). Divide the final decimal number once more until it equals 0, and record the results of each division. The remainders, which are equivalent hexadecimal numbers to the given decimal number, should then be written from bottom to top (or in reverse order).
The following procedures must be used to perform some fundamental mathematical calculations in order to convert decimal to hexadecimal in c++:
Step 1: First, divide the value in the given decimal number system by 16, then note the result.
Step 2: Divide the quotient by 16 in step two. Continue doing this until the quotient is equal to zero.
Step 3: Replace the numbers 10, 11, 12, 13, 14, and 15 in the remainders, respectively, with the letters A, B, C, D, E, and F.
Step 4: Arrange all of the values of the remainder using the reverse order pattern.
The obtained number is the necessary hexadecimal number,
The formula for converting the given numbers from decimal to hexadecimal is,
P10 = Q16
a decimal number P and a hexadecimal number Q are present.
With the aid of the following example, let's learn how to change a decimal number into a hexadecimal number.
An example would be to hexadecimal the number 5386.
Implementation in C++
// converting decimal to hexadecimal in c++
#include <bits/stdc++.h>
using namespace std;
// function to convert decimal to hexadecimal in c++
string convertHexa(int number)
{
string hexa;
// loop till number>0
while (number)
{
int rem = number % 16;
// when rem is less than 10 then store 0-9
// else store A - F
if (rem < 10)
hexa.push_back(rem + '0');
else
hexa.push_back(rem - 10 + 'A');
number = number / 16;
}
reverse(hexa.begin(), hexa.end());
return hexa;
}
// drive code
int main()
{
int number; // store decimal number
cin >> number;
string hexa; // store hexadecimal
// calling function to convert decimal to hexadecimal in c++
hexa = convertHexa(number);
// printing hexadecimal;
cout << hexa << endl;
}
Input:
7654
Output:
1DE6
Try and compile by yourself with the help of online C++ Compiler for better understanding.
Complexity Analysis
Time Complexity: O(log16(number)), because we divide the number by 16 till it becomes zero.
Space Complexity: O(1), we cannot use any extra space.