Skip to content

Instantly share code, notes, and snippets.

@aeppert
Last active September 9, 2015 14:11
Show Gist options
  • Select an option

  • Save aeppert/1528ccf8e9c8d6b44912 to your computer and use it in GitHub Desktop.

Select an option

Save aeppert/1528ccf8e9c8d6b44912 to your computer and use it in GitHub Desktop.
to_hex template
// Header:
// template <class T> std::string to_hex(T t, std::string prefix = "", bool pad = true, int8 custom_len = -1);
#include <sstream>
#include <string>
#include <limits>
template <class T> std::string to_hex(T t, std::string prefix, bool pad, int8 custom_len)
{
std::ostringstream oss;
if(pad) {
oss.fill('0');
if(custom_len < 0) {
oss.width(std::numeric_limits<T>::digits/4);
} else {
oss.width(custom_len);
}
}
oss << std::hex << t;
prefix += oss.str();
return prefix;
}
template std::string to_hex<time_t>(time_t t, std::string prefix, bool pad, int8 custom_len);
template std::string to_hex<uint32>(uint32 t, std::string prefix, bool pad, int8 custom_len);
template std::string to_hex<uint16>(uint16 t, std::string prefix, bool pad, int8 custom_len);
template std::string to_hex<uint8>(uint8 t, std::string prefix, bool pad, int8 custom_len);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment