Open In App

C++ Program For Decimal To Hexadecimal Conversion

Last Updated : 04 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn to write a C++ program to convert a decimal number into an equivalent hexadecimal number. i.e. convert the number with base value 10 to base value 16.

In the decimal system, we use ten digits (0 to 9) to represent a number, while in the hexadecimal system, we use sixteen symbols (0 to 9 and A to F) to represent a number.

Algorithm for Decimal to Hexadecimal Conversion

  1. Initialize a character array hexaDeciNum to store the hexadecimal representation.
  2. Run a loop till n is non-zero.
  3. Inside the loop,
    1. Initialize an integer variable temp to store the remainder and a character variable ch to store the converted hexadecimal character.
    2. Find the remainder of the number by taking mod by 16 (base of the hexadecimal system).
    3. Check if temp < 10, convert it to the corresponding ASCII character for the digit by adding 48 to the character, and store it in hexaDeciNum. (‘0’ to ‘9’ => ASCII 48 to 57).
    4. Else, convert the current decimal number to the corresponding ASCII character for the digit by adding 55 to the digit, and storing it in hexaDeciNum.
  4. Update the number by dividing it by 16.
  5. Print the character array in reverse order.

The below diagram shows an example of converting the decimal number 2545 to an equivalent hexadecimal number.  

C++ Program to Convert Decimal Number To Hexadecimal

C++




// C++ program to convert a decimal
// number to hexadecimal number
#include <iostream>
using namespace std;
  
// Function to convert decimal
// to hexadecimal
void decToHexa(int n)
{
    // char array to store hexadecimal number
    char hexaDeciNum[100];
  
    // Counter for hexadecimal number array
    int i = 0;
    while (n != 0) {
        // Temporary variable to store remainder
        int temp = 0;
  
        // Storing remainder in temp variable.
        temp = n % 16;
  
        // Check if temp < 10
        if (temp < 10) {
            hexaDeciNum[i] = temp + 48;
            i++;
        }
        else {
            hexaDeciNum[i] = temp + 55;
            i++;
        }
  
        n = n / 16;
    }
  
    // Printing hexadecimal number
    // array in reverse order
    for (int j = i - 1; j >= 0; j--)
        cout << hexaDeciNum[j];
}
  
// Driver code
int main()
{
    int n = 2545;
    decToHexa(n);
    return 0;
}


Output

9F1

Explanation

If the given decimal number is 2545.

  • Step 1: Calculate the remainder when 2545 is divided by 16 is 1. Therefore, temp = 1. As temp is less than 10. So, arr[0] = 48 + 1 = 49 = ‘1’. 
  • Step 2: Divide 2545 by 16. The new number is 2545/16 = 159. 
  • Step 3: Calculate the remainder when 159 is divided by 16 is 15. Therefore, temp = 15. As temp is greater than 10. So, arr[1] = 55 + 15 = 70 = ‘F’. 
  • Step 4: Divide 159 by 16. The new number is 159/16 = 9. 
  • Step 5: Calculate the remainder when 9 is divided by 16 is 9. Therefore, temp = 9. As temp is less than 10. So, arr[2] = 48 + 9 = 57 = ‘9’. 
  • Step 6: Divide 9 by 16. The new number is 9/16 = 0. 
  • Step 7: Since the number becomes = 0. Stop repeating steps and print the array in reverse order. Therefore, the equivalent hexadecimal number is 9F1.

decimal to hexadecimal conversion

Complexity Analysis

  • Time complexity: O(log16n)
  • Auxiliary space: O(1)

Refer to the complete article Program for decimal to hexadecimal conversion for more details.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads