It’s easy to hex encode a string using a stringstream, but is it possible to do the reverse and decode the resulting string with a stringstream?
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
int main()
{
std::string test = "hello, world";
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (unsigned ch : test)
ss << std::setw(2) << int(ch);
std::cout << ss.str() << std::endl;
}
I’m not looking to bit shift the bytes directly or use old c functions such as the scanf family of functions.
Not quite as easy, if you just have a stream of hexadecimal digits with no separator. You can use
std::basic_istream::getorstd::basic_istream::readto extract two digits at a time, and then use e.g.std::stoito convert to an integer which can then be type-casted to achar.