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