Skip to content

Instantly share code, notes, and snippets.

@kidapu
Created June 7, 2016 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidapu/0d1bfdb2ad746fb3df9e444f4a2302d1 to your computer and use it in GitHub Desktop.
Save kidapu/0d1bfdb2ad746fb3df9e444f4a2302d1 to your computer and use it in GitHub Desktop.
floatやintから16進数化したstringが得られる。あんまり使うことなさそう。
class Utils
{
private:
template< typename T >
static std::string IntToHex( T i )
{
std::stringstream stream;
stream
<< std::setfill ('0')
<< std::setw(sizeof(T)*2)
<< std::hex << i;
return stream.str();
}
static std::string FloatToHex(float val)
{
union converter{
float f_val;
unsigned int u_val;
};
union converter var;
var.f_val = val;
return IntToHex(var.u_val);
}
public:
template <typename T>
static std::string ToHex(T i)
{
bool isFloat = bool(typeid(i) == typeid(float));
std::string out = (isFloat) ? FloatToHex(i) : IntToHex(i);
return out;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment