Created
March 24, 2019 19:21
-
-
Save ivanstepanovftw/9159efe123140c45d9df02e3d0454152 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** @arg what: any number | |
* @return: string number represented as hex */ | |
template <typename T, std::endian endianess = std::endian::native> | |
std::string HEX__1(const T& value, size_t value_size = sizeof(T)) | |
{ | |
using namespace std; | |
uint8_t *buffer = (uint8_t*)(&value); | |
char converted[value_size * 2 + 1]; | |
if (endianess == std::endian::big) | |
for(size_t i = 0; i < value_size; ++i) { | |
sprintf(&converted[i*2], "%02X", buffer[i]); | |
} | |
else | |
for(size_t i = 0; i < value_size; ++i) { | |
sprintf(&converted[i*2], "%02X", buffer[value_size-1-i]); | |
} | |
return converted; | |
} | |
void main1 () { | |
using namespace std; | |
{ | |
uint8_t a = 0x10; | |
cout<<HEX__1(a)<<endl; | |
if (HEX__1(a) != "10") throw std::runtime_error(""); | |
}{ | |
int32_t a = -128; | |
cout<<HEX__1(a)<<endl; | |
if (HEX__1(a) != "FFFFFF80") throw std::runtime_error(""); | |
}{ | |
uint64_t a = 0x0000000000000102; | |
cout<<HEX__1(a)<<endl; | |
if (HEX__1(a) != "0000000000000102") throw std::runtime_error(""); | |
}{ | |
double a = 1; | |
cout<<HEX__1(a)<<endl; | |
if (HEX__1(a) != "3FF0000000000000") throw std::runtime_error(""); | |
} | |
cout<<"------------------"<<endl; | |
{ | |
uint64_t a = 0; | |
cout<<HEX__1(a)<<endl; | |
if (HEX__1(a) != "0000000000000000") throw std::runtime_error(""); | |
} | |
} | |
int main() { | |
main1(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment