Decimal Number to Hexadecimal Number Conversion in C++
Taking a decimal number input from the user, and then performing the decimal to hexadecimal conversion on it, and displaying the final converted result with the help of the C++ programming language is the purpose of this tutorial.
Decimal numbers have a base of 10 as they use digits 0 – 9 to represent any value.
Similarly, Hexadecimal numbers have a base of 16 as they use 16 values to represent any number, 0 – 9 represent digits 0 – 9, and A – F represent 10 – 15.
Few examples of hex equivalents of decimal numbers are:
(116)₁₀ = (74)₁₆
(10)₁₀ = (A)₁₆
(33)₁₀ = (21)₁₆
(2545)₁₀ = (9F1)₁₆
Decimal numbers are represented in base 10, while hexadecimal numbers are represented in base 16.
Algorithm for Decimal to Hexadecimal Conversion
- We start by making a character array to store the hexadecimal number.
- Take the number in the decimal form that has to be converted into hexadecimal.
- Store the value in a temporary variable which would come as a result of the decimal number being divided by 16.
- If the remainder is between 0 – 9 then, insert (48 + temporary variable value) in the hex array.
- Else if the remainder is between 10 – 15 then, insert (55 + temporary variable value) in the hex array.
- Now update the variable which has the decimal value with the value of (decimal number / 16).
- Repeat the same steps given above again until the number is not equal to zero after updating.
- If the number is equal to zero that means that the conversion is over and we print the result now.
- Print the hex array in reverse order to get the final answer after conversion.
C++ code for Decimal to Hexadecimal Conversion
#include <iostream> using namespace std; //function to convert decimal to hexadecimal void Decimal_to_Hexadecimal(int decimal_number) { //variables used int remainder, j, i=0; //we take a char array as hexadecimal numbers include //digits as well as characters char Hexa_Num[50]; //loop for the actual conversion while(decimal_number != 0) { remainder = decimal_number % 16; //storing hex value according to remainder //remainder < 10 implies 0-9 values in hex if(remainder < 10) { Hexa_Num[i] = remainder + 48; i++; } //remainder >= 10 (and <16) implies A-F values in hex else { Hexa_Num[i] = remainder + 55; i++; } decimal_number = decimal_number/16; } //printing hexadecimal number in reverse order for(j= (i-1) ; j>=0 ; j--) { cout<<Hexa_Num[j]; } cout<<endl; } //int main int main() { int decimal_number; //taking the decimal value from the user cout<<"Enter the decimal number to convert in hexadecimal: "<<endl; cin>>decimal_number; cout<<"\nThe hexadecimal number after conversion is: "<<endl; //calling the function to convert the number Decimal_to_Hexadecimal(decimal_number); return 0; }
Output
Enter the decimal number you want to change in hexadecimal: 245862 The corresponding hexadecimal number after conversion is: 3C066
To learn more about conversions among various number systems and their types you can check out the following links too!
Leave a Reply