From 755d08016164aad50fd78fd425be46ad9c57b15f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 18 Apr 2018 12:25:18 +0300 Subject: [PATCH] MXS-1810: Add C++ hex conversion functions Added mxs::to_hex for uint8_t types and uint8_t containers. --- include/maxscale/utils.hh | 44 +++++++++++++++++++++++++++++++++++++++ server/core/utils.cc | 14 +++++++++++++ 2 files changed, 58 insertions(+) diff --git a/include/maxscale/utils.hh b/include/maxscale/utils.hh index 9aabb9b14..e1736a55f 100644 --- a/include/maxscale/utils.hh +++ b/include/maxscale/utils.hh @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include namespace maxscale @@ -296,4 +298,46 @@ EqualPointees equal_pointees(const T& t) return EqualPointees(t); } +/** + * Get hexadecimal string representation of @c value + * + * @param value Value to convert + * + * @return Hexadecimal string representation of @c value + */ +std::string to_hex(uint8_t value); + +template +struct hex_iterator +{ +}; + +template +struct hex_iterator +{ + std::string operator()(T begin, T end) + { + std::string rval; + for (auto it = begin; it != end; it++) + { + rval += to_hex(*it); + } + return rval; + } +}; + +/** + * Create hexadecimal representation of a type + * + * @param begin Starting iterator + * @param end End iterator + * + * @return Hexadecimal string representation of the data + */ +template +std::string to_hex(Iter begin, Iter end) +{ + return hex_iterator::value_type > ()(begin, end); +} + } diff --git a/server/core/utils.cc b/server/core/utils.cc index ccab2f6be..618308c2c 100644 --- a/server/core/utils.cc +++ b/server/core/utils.cc @@ -29,6 +29,7 @@ */ #include +#include #include #include @@ -1136,3 +1137,16 @@ long get_processor_count() #endif return processors; } + +namespace maxscale +{ + +std::string to_hex(uint8_t value) +{ + std::string out; + out += hex_lower[value >> 4]; + out += hex_lower[value & 0x0F]; + return out; +} + +}