StringStream in C++ for Decimal to Hexadecimal and back Last Updated : 14 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 manipulation. Here is a quick way to convert any decimal to hexadecimal using stringstream: CPP // CPP program to convert integer to // hexadecimal using stringstream and // hex I/O manipulator. #include <bits/stdc++.h> using namespace std; int main() { int i = 942; stringstream ss; ss << hex << i; string res = ss.str(); cout << "0x" << res << endl; // this will print 0x3ae return 0; } Output: 0x3ae The time complexity of this program is O(1) as it only performs a single operation. The space complexity is also O(1) as no additional space is used. If we want to change hexadecimal string back to decimal you can do it by following way: CPP // CPP program to convert hexadecimal to // integer using stringstream and // hex I/O manipulator. #include <bits/stdc++.h> using namespace std; int main() { string hexStr = "0x3ae"; unsigned int x; stringstream ss; ss << std::hex << hexStr; ss >> x; cout << x << endl; // this will print 942 return 0; } Output: 942 Time Complexity: The time complexity of the above algorithm is O(1), as we are only performing a few operations. Space Complexity: The space complexity of the above algorithm is O(1), as we are only using a few variables. Comment More infoAdvertise with us Next Article Program for Decimal to Octal Conversion U Utkarsh Sinha Follow Improve Article Tags : C++ cpp-input-output cpp-string base-conversion cpp-stringstream +1 More Practice Tags : CPP Similar Reads Program for Hexadecimal to Decimal Given a hexadecimal number as input, we need to write a program to convert the given hexadecimal number into an equivalent decimal number.Examples: Input : 67 Output: 103 Input : 512 Output: 1298 Input : 123 Output: 291Hexadecimal NumberWe know that hexadecimal number uses 16 symbols {0, 1, 2, 4, 5, 9 min read How To Convert a Qstring to Hexadecimal in C++? In CPP Qt5 has a QString class for working with strings. It is very powerful and has numerous methods, A Unicode character string is made available via the QString class. A string is kept as a 16-bit QChar. Unicode 4.0 characters are represented by one QChar each. A QString can be changed, unlike st 2 min read Convert Hexadecimal value String to ASCII value String Given the Hexadecimal value string as input, the task is to convert the given hexadecimal value string into its corresponding ASCII format string. Examples:Â Input: 6765656b73Output: geeks Input: 6176656e67657273Output: avengers The âHexadecimalâ or simply âHexâ numbering system uses the Base of 16 6 min read Program to convert a binary number to hexadecimal number Given a Binary Number, the task is to convert the given binary number to its equivalent hexadecimal number. The input could be very large and may not fit even into an unsigned long long int.Examples:Â Input: 110001110Output: 18EInput: 1111001010010100001.010110110011011Output: 794A1.5B36 794A1D9B App 13 min read Program for Decimal to Octal Conversion Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent octal number. i.e convert the number with base value 10 to base value 8. The base value of a number system determines the number of digits used to represent a numeric value. For example 10 min read 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 equivalent decimal number. Examples: Input : 67Output: 55Input : 512Output: 330Input : 123Output: 83The idea is to extract the digits of a given octal number starting from the rightmost digit and keep a 11 min read Like