Created
May 13, 2015 09:54
-
-
Save alepez/3dccec1484a7ed0bf62b to your computer and use it in GitHub Desktop.
hex encode
This file contains hidden or 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
#include <string> | |
#include <vector> | |
#include <iostream> | |
using byte = uint8_t; | |
static std::string hexEncode(const std::vector<uint8_t>& data) { | |
std::string result(data.size() * 3, ' '); | |
for (size_t i = 0, k = 0; k < result.size(); ++i, k += 3) { | |
static const char hex[] = "0123456789abcdef"; | |
const uint8_t val = data[i]; | |
result[k + 0] = hex[val >> 4]; | |
result[k + 1] = hex[val & 15]; | |
} | |
return result; | |
} | |
int main() { | |
std::string test("CIAO"); | |
std::vector<uint8_t> vtest(test.begin(), test.end()); | |
std::cerr << hexEncode(vtest) << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment