Skip to content

Instantly share code, notes, and snippets.

@alepez
Created May 13, 2015 09:54
Show Gist options
  • Save alepez/3dccec1484a7ed0bf62b to your computer and use it in GitHub Desktop.
Save alepez/3dccec1484a7ed0bf62b to your computer and use it in GitHub Desktop.
hex encode
#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