1 #pragma once 2 3 #include <array> 4 #include <cstddef> 5 #include <string> 6 7 template <typename IntegerType> 8 inline std::string intToHexString(IntegerType value, 9 size_t digits = sizeof(IntegerType) << 1) 10 { 11 static constexpr std::array<char, 16> digitsArray = { 12 '0', '1', '2', '3', '4', '5', '6', '7', 13 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 14 std::string rc(digits, '0'); 15 size_t bitIndex = (digits - 1) * 4; 16 for (size_t digitIndex = 0; digitIndex < digits; digitIndex++) 17 { 18 rc[digitIndex] = digitsArray[(value >> bitIndex) & 0x0f]; 19 bitIndex -= 4; 20 } 21 return rc; 22 } 23