MXS-1810: Add C++ hex conversion functions
Added mxs::to_hex for uint8_t types and uint8_t containers.
This commit is contained in:
@ -15,6 +15,8 @@
|
|||||||
#include <maxscale/cppdefs.hh>
|
#include <maxscale/cppdefs.hh>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <iterator>
|
||||||
|
#include <string>
|
||||||
#include <tr1/unordered_map>
|
#include <tr1/unordered_map>
|
||||||
|
|
||||||
namespace maxscale
|
namespace maxscale
|
||||||
@ -296,4 +298,46 @@ EqualPointees<T> equal_pointees(const T& t)
|
|||||||
return EqualPointees<T>(t);
|
return EqualPointees<T>(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 <typename T, typename V>
|
||||||
|
struct hex_iterator
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct hex_iterator<T, uint8_t>
|
||||||
|
{
|
||||||
|
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 <typename Iter>
|
||||||
|
std::string to_hex(Iter begin, Iter end)
|
||||||
|
{
|
||||||
|
return hex_iterator<Iter, typename std::iterator_traits<Iter>::value_type > ()(begin, end);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,6 +29,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <maxscale/utils.h>
|
#include <maxscale/utils.h>
|
||||||
|
#include <maxscale/utils.hh>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
@ -1136,3 +1137,16 @@ long get_processor_count()
|
|||||||
#endif
|
#endif
|
||||||
return processors;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user