Created
June 20, 2019 07:20
-
-
Save sjolsen/eb11a7b59f2f12c4f1235ca4793e3e99 to your computer and use it in GitHub Desktop.
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
#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