# StringStream in C++ for Decimal to Hexadecimal and back

By treating strings as input/output streams, the flexible C++ class std::stringstream from the <sstream> header makes string manipulation easier. Std::stringstream is especially helpful for formatting data and converting between different data types. It makes tasks like converting decimal numbers to hexadecimal and vice versa easier to do. The std::hex manipulator turns decimal values into hexadecimal representations, making it simple to insert decimal values into the stringstream when converting them from decimal to hexadecimal. In contrast, the conversion from hexadecimal to decimal is simple because the stringstream reads the hexadecimal string as input. This method, which makes use of std::stringstream, improves the readability and maintainability of the code while offering a useful and effective tool for C++ string operations.

C++ Code:

`//C++ program for Decimal to Hexadecimal using the Stringstream function.#include <iostream>#include <sstream>int main() { int decimalNumber = 255; // Create a stringstream std::stringstream ss; // Insert the decimal number into the stringstream as if it were an output stream ss << std::hex << decimalNumber; // Extract the hexadecimal representation as a string std::string hexString = ss.str(); // Print the result std::cout << "Decimal to Hexadecimal: " << hexString << std::endl; return 0;}`

Output:

`Decimal to Hexadecimal: ff`

Example 2:

`//C++ program for Decimal to Hexadecimal using the Stringstream function.#include <iostream>#include <sstream>int main() { std::string hexString = "FF"; // Create a stringstream std::stringstream ss; // Insert the hexadecimal string into the stringstream as if it were an input stream ss << std::hex << hexString; // Extract the decimal representation int decimalNumber; ss >> decimalNumber; // Print the result std::cout << "Hexadecimal to Decimal: " << decimalNumber << std::endl; return 0;}`

Output:

`Hexadecimal to Decimal: 255`

Example 3:

`#include <iostream>#include <sstream>int main() { // Convert Decimal to Hexadecimal int decimalNumber = 56789; std::stringstream decimalToHexStream; decimalToHexStream << std::hex << decimalNumber; std::string hexadecimalResult = decimalToHexStream.str(); std::cout << "Decimal to Hexadecimal: " << hexadecimalResult << std::endl; // Convert Hexadecimal to Decimal std::string hexadecimalString = "DEADBEEF"; std::stringstream hexToDecimalStream(hexadecimalString); int decimalResult; hexToDecimalStream >> std::hex >> decimalResult; std::cout << "Hexadecimal to Decimal: " << decimalResult << std::endl; return 0;}`

Output:

`Decimal to Hexadecimal: ddd5Hexadecimal to Decimal: 2147483647`

Example 4:

`#include <iostream>#include <sstream>#include <iomanip> // For std::setw#include <limits> // For std::numeric_limitsint main() { // Convert Decimal to Hexadecimal int decimalNumber; std::cout << "Enter a decimal number: "; // Input validation using a loop while (!(std::cin >> decimalNumber)) { std::cin.clear(); // Clear error flags std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input std::cout << "Invalid input. Please enter a valid decimal number: "; } std::stringstream decimalToHexStream; decimalToHexStream << std::hex << decimalNumber; std::string hexadecimalResult = decimalToHexStream.str(); std::cout << "Decimal to Hexadecimal: 0x" << std::setw(8) << std::setfill('0') << hexadecimalResult << std::endl; // Convert Hexadecimal to Decimal std::string hexadecimalString; std::cout << "Enter a hexadecimal number (without '0x' prefix): "; std::cin >> std::setw(8) >> hexadecimalString; // Read at most 8 characters std::stringstream hexToDecimalStream(hexadecimalString); int decimalResult; // Input validation for hexadecimal conversion if (!(hexToDecimalStream >> std::hex >> decimalResult)) { std::cout << "Invalid hexadecimal input." << std::endl; } else { std::cout << "Hexadecimal to Decimal: " << decimalResult << std::endl; } return 0;}`

Output:

`Enter a decimal number: 687Decimal to Hexadecimal: 0x000002afEnter a hexadecimal number (without '0x' prefix): 3bfHexadecimal to Decimal: 959`

Example 5:

`#include <iostream>#include <sstream>#include <iomanip> // For std::setw#include <limits> // For std::numeric_limitsint main() { while (true) { std::cout << "Choose an option:\n" << "1. Decimal to Hexadecimal\n" << "2. Hexadecimal to Decimal\n" << "3. Quit\n"; int choice; std::cout << "Enter your choice: "; std::cin >> choice; if (std::cin.fail()) { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "Invalid input. Please enter a number.\n"; continue; } if (choice == 1) { // Convert Decimal to Hexadecimal int decimalNumber; std::cout << "Enter a decimal number: "; // Input validation using a loop while (!(std::cin >> decimalNumber)) { std::cin.clear(); // Clear error flags std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input std::cout << "Invalid input. Please enter a valid decimal number: "; } std::stringstream decimalToHexStream; decimalToHexStream << std::hex << decimalNumber; std::string hexadecimalResult = decimalToHexStream.str(); std::cout << "Decimal to Hexadecimal: 0x" << std::setw(8) << std::setfill('0') << hexadecimalResult << std::endl; } else if (choice == 2) { // Convert Hexadecimal to Decimal std::string hexadecimalString; std::cout << "Enter a hexadecimal number (without '0x' prefix): "; std::cin >> std::setw(8) >> hexadecimalString; // Read at most 8 characters std::stringstream hexToDecimalStream(hexadecimalString); int decimalResult; // Input validation for hexadecimal conversion if (!(hexToDecimalStream >> std::hex >> decimalResult)) { std::cout << "Invalid hexadecimal input." << std::endl; } else { std::cout << "Hexadecimal to Decimal: " << decimalResult << std::endl; } } else if (choice == 3) { std::cout << "Exiting the program. Goodbye!\n"; break; } else { std::cout << "Invalid choice. Please enter a valid option.\n"; } } return 0;}`

Output:

`Choose an option:1. Decimal to Hexadecimal2. Hexadecimal to Decimal3. QuitEnter your choice: 1Enter a decimal number: 234Decimal to Hexadecimal: 0x000000eaChoose an option:1. Decimal to Hexadecimal2. Hexadecimal to Decimal3. QuitEnter your choice:`

## Conclusion:

If you want to convert between decimal and hexadecimal representations with ease, the C++ std::stringstream class, which you can access via the <sstream> header, is a strong and flexible tool. With the help of its functionality, data manipulation and formatting can be made simpler by treating strings as input and output streams.

As hexadecimal representations, decimal values can be easily inserted into the stringstream using the std::hex manipulator when converting from decimal to hexadecimal. Likewise, the stringstream reads the hexadecimal string as input and provides an easy-to-use conversion mechanism when converting from hexadecimal to decimal.