C++ Program For Decimal To Hexadecimal Conversion Last Updated : 04 Aug, 2023 Comments Improve Suggest changes Like Article Like 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 ConversionInitialize a character array hexaDeciNum to store the hexadecimal representation.Run a loop till n is non-zero.Inside the loop,Initialize an integer variable temp to store the remainder and a character variable ch to store the converted hexadecimal character.Find the remainder of the number by taking mod by 16 (base of the hexadecimal system).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).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.Update the number by dividing it by 16.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; } Output9F1ExplanationIf 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. Complexity AnalysisTime complexity: O(log16n)Auxiliary space: O(1)Refer to the complete article Program for decimal to hexadecimal conversion for more details. Comment More infoAdvertise with us Next Article C++ Program For Decimal To Hexadecimal Conversion kartik Follow Improve Article Tags : C++ Programs C++ C++ Conversion Programs Practice Tags : CPP Similar Reads C++ Program For Hexadecimal To Decimal Conversion The hexadecimal numbers are base 16 numbers that use 16 symbols {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} to represent all digits. Here, (A, B, C, D, E, F) represents (10, 11, 12, 13, 14, 15). Decimal numbers are base 10 numbers with 10 symbols to represent all digits. In this article, we will l 3 min read C++ Program For Decimal To Octal Conversion The octal numbers are a base 8 number system that uses digits from 0-7 and the decimal numbers are a base 10 numbers system that uses 10 digits from 0-9 to represent any numeric value. In this article, we will learn how to write a C++ program to convert a given decimal number into an equivalent octa 2 min read C++ Program For Octal To Decimal Conversion Given an octal number as input, we need to write a program to convert the given octal number into an equivalent decimal number. Examples: Input : 67Output: 55 Input : 512Output: 330 Input : 123Output: 83 1. Simple ApproachThe idea is to extract the digits of a given octal number starting from the ri 2 min read C++ Program For Binary To Decimal Conversion The binary number system uses only two digits 0 and 1 to represent an integer and the Decimal number system uses ten digits 0 to 9 to represent a number. In this article, we will discuss the program for Binary to Decimal conversion in C++. Algorithm to Convert Binary Numbers to DecimalInitialize a v 4 min read C++ Program For Decimal To Binary Conversion Binary Numbers uses only 0 and 1 (base-2), while Decimal Number uses 0 to 9 (base-10). In this article, we will learn to implement a C++ program to convert Decimal numbers to Binary Numbers. The below diagram shows an example of converting the decimal number 17 to an equivalent binary number. Recomm 3 min read StringStream in C++ for Decimal to Hexadecimal and back Stringstream is stream class present in C++ which is used for doing operations on a string. It can be used for formatting/parsing/converting a string to number/char etc. Hex is an I/O manipulator that takes reference to an I/O stream as parameter and returns reference to the stream after manipulatio 2 min read C++ Program For Binary To Octal Conversion The problem is to convert the given binary number (represented as a string) to its equivalent octal number. The input could be very large and may not fit even into an unsigned long long int. Examples: Input: 110001110Output: 616 Input: 1111001010010100001.010110110011011Output: 1712241.26633 Simple 5 min read C++ Program For char to int Conversion In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.Example:Input: '9'Output: 9Ex 2 min read C++ Program For Double to String Conversion Here, we will build a C++ program for double to string conversion using various methods i.e. Using to_stringUsing stringstreamUsing sprintfUsing lexical_cast We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 456321.7651234 Output: string: 456321.76 2 min read Program to convert IP address to hexadecimal Given an IP Address and task is to change the IP address equivalent to the hexadecimal value. Examples: Input : 127.0.0.1Output : 0x7f000001Input : 172.31.0.2Output : 0xac1f0002Explanation: Using the Library function to convert the IP address into the hexadecimal value we use the " arpa/inet.h " hea 4 min read Like