Last active
September 10, 2021 01:25
-
-
Save waveacme/d0b5e8803675be3f10596cb4124b0d88 to your computer and use it in GitHub Desktop.
c++ version of bin2hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://stackoverflow.com/questions/18424101/c-small-char-to-hex-function | |
#include <string> | |
#include <iostream> | |
std::string bin2hex(const std::string& input) | |
{ | |
std::string res; | |
const char hex[] = "0123456789ABCDEF"; | |
for(auto sc : input) | |
{ | |
unsigned char c = static_cast<unsigned char>(sc); | |
res += hex[c >> 4]; | |
res += hex[c & 0xf]; | |
} | |
return res; | |
} | |
int main() | |
{ | |
std::string example = "A string"; | |
std::cout << "bin2hex of " << example << " gives " << bin2hex(example) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/18906027/missing-punctuation-from-c-hex2bin