Skip to content

Instantly share code, notes, and snippets.

@sjolsen
Created June 20, 2019 07:20
Show Gist options
  • Save sjolsen/eb11a7b59f2f12c4f1235ca4793e3e99 to your computer and use it in GitHub Desktop.
Save sjolsen/eb11a7b59f2f12c4f1235ca4793e3e99 to your computer and use it in GitHub Desktop.
#include <charconv>
#include <iostream>
#include <string>
#include <system_error>
std::string to_hex(uint64_t val) {
char buf[16];
auto [end, e] = std::to_chars(std::begin(buf), std::end(buf), val, 16);
if (e != std::errc()) {
throw std::system_error(std::make_error_code(e));
}
return {buf, end};
}
int main() {
try {
std::cout << to_hex(0x1234'DEAD'BEEF) << std::endl;
return EXIT_SUCCESS;
} catch (const std::system_error& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment