Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert stringized hex character to std::string

Tags:

c++

string

boost

I have the following:

char const* code = "3D";

I need to convert this 2-digit lexical hex into a std::string, which will be a string with length of 1 (not including null terminator). I have the boost library at my disposal as well. How can I do this?

In the example above, I should have a std::string that prints "=" if properly converted.

like image 845
void.pointer Avatar asked May 16 '26 05:05

void.pointer


1 Answers

I think something on this order should work:

std::istringstream buffer("3D");
int x;

buffer >> std::hex >> x;
std::string result(1, (char)x);

std::cout << result;  // should print "="
like image 158
Jerry Coffin Avatar answered May 18 '26 19:05

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!