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