Created
June 7, 2016 11:15
-
-
Save kidapu/0d1bfdb2ad746fb3df9e444f4a2302d1 to your computer and use it in GitHub Desktop.
floatやintから16進数化したstringが得られる。あんまり使うことなさそう。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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