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

  1. We start by making a character array to store the hexadecimal number.
  2. Take the number in the decimal form that has to be converted into hexadecimal.
  3. Store the value in a temporary variable which would come as a result of the decimal number being divided by 16.
  4. If the remainder is between 0 – 9 then, insert (48 + temporary variable value) in the hex array.
  5. Else if the remainder is between 10 – 15 then, insert (55 + temporary variable value) in the hex array. 
  6. Now update the variable which has the decimal value with the value of (decimal number / 16).
  7. Repeat the same steps given above again until the number is not equal to zero after updating.
  8. If the number is equal to zero that means that the conversion is over and we print the result now.
  9. 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!

Binary to Decimal conversion in C++

Decimal to Octal conversion in C++

Leave a Reply

Your email address will not be published. Required fields are marked *