In python if I have i = '0000000000000000d0ed77c1f924ac20'
, I can run i.decode('hex')
to get the string '\x00\x00\x00\x00\x00\x00\x00\x00\xd0\xedw\xc1\xf9$\xac '
. I have searched all over for the equivalent function in C/C++ to no avail. Is there some library which performs this equivalent function or how can I implement it myself? I know it shows the non-printable characters as hex and the printable ones it just prints - but I need the exact same implementation as in Python and I'm not sure if I made a table myself I would get it right.
Asked
Active
Viewed 807 times
-1
-
1https://stackoverflow.com/questions/3790613/how-to-convert-a-string-of-hex-values-to-a-string https://stackoverflow.com/questions/1487440/convert-hexadecimal-string-with-leading-0x-to-signed-short-in-c?noredirect=1&lq=1 – Rick_C137 Apr 13 '18 at 05:44
-
1Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Then you will learn that asking for libraries is off-topic. And no matter what, unless you copy it from the Python code you will never get the "EXACT" same implementation. – Some programmer dude Apr 13 '18 at 05:45
1 Answers
0
There is std::hex
manipulator in C++ for cobverting hexadecimal int. And if you have a hexadecimal string, then you can use std::stoul
(introduced in C++11).
int main()
{
std::string s = "0xfffefffe";
long long int x = std::stoul(s, nullptr, 16);
std::cout << x << "\n";
long long input_hex = 0xfffefffe;
std::cin >> std::hex >> input_hex;
std::cout << input_hex << std::endl;
return 0;
}

Abhishek Keshri
- 3,074
- 14
- 31