diff --git a/README.md b/README.md index add00b2e..326ee5ff 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The accompanying package names are from the Debian (APT) package repositories - repositories. - CMake version 3.22 or later -- G++10 or later +- G++13 or later - libusb v1.0 (`libusb-1.0-0-dev`) - libhidapi (0.11.2 or later) (`libhidapi-dev`) - yaml-cpp (version 0.7.0 or later) (`libyaml-cpp-dev`) diff --git a/src/Services/StringService.cpp b/src/Services/StringService.cpp index 3125d41d..2361457e 100644 --- a/src/Services/StringService.cpp +++ b/src/Services/StringService.cpp @@ -64,30 +64,6 @@ namespace Services }); } - std::string StringService::toHex(std::uint64_t value) { - auto stream = std::stringstream{}; - stream << std::hex << std::setfill('0') << std::setw(16) << static_cast(value); - return stream.str(); - } - - std::string StringService::toHex(std::uint32_t value) { - auto stream = std::stringstream{}; - stream << std::hex << std::setfill('0') << std::setw(8) << static_cast(value); - return stream.str(); - } - - std::string StringService::toHex(std::uint16_t value) { - auto stream = std::stringstream{}; - stream << std::hex << std::setfill('0') << std::setw(4) << static_cast(value); - return stream.str(); - } - - std::string StringService::toHex(unsigned char value) { - auto stream = std::stringstream{}; - stream << std::hex << std::setfill('0') << std::setw(2) << static_cast(value); - return stream.str(); - } - std::string StringService::toHex(std::span data) { auto stream = std::stringstream{}; stream << std::hex << std::setfill('0'); diff --git a/src/Services/StringService.hpp b/src/Services/StringService.hpp index dc7b5367..6604d41c 100644 --- a/src/Services/StringService.hpp +++ b/src/Services/StringService.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace Services { @@ -23,10 +24,29 @@ namespace Services static bool isNumeric(std::string_view str); static bool isBinary(std::string_view str); - static std::string toHex(std::uint64_t value); - static std::string toHex(std::uint32_t value); - static std::string toHex(std::uint16_t value); - static std::string toHex(unsigned char value); + template + requires std::is_integral_v + static std::string toHex(Type value) { + if constexpr (sizeof(Type) == 1) { + return std::format("{:02X}", value); + } + + if constexpr (sizeof(Type) == 2) { + return std::format("{:04X}", value); + } + + if constexpr (sizeof(Type) == 4) { + return std::format("{:08X}", value); + } + + if constexpr (sizeof(Type) == 8) { + return std::format("{:016X}", value); + + } else { + return std::format("{:X}", value); + } + } + static std::string toHex(std::span data); static std::string toHex(std::string_view data);