Split byte order routines out of utils.h

This commit is contained in:
Vinnie Falco
2013-05-25 16:32:08 -07:00
parent 4e8d4c28c2
commit 61fb6538d7
8 changed files with 122 additions and 73 deletions

View File

@@ -0,0 +1,41 @@
#ifdef WIN32
//#include <windows.h>
// from: http://stackoverflow.com/questions/3022552/is-there-any-standard-htonl-like-function-for-64-bits-integers-in-c
// but we don't need to check the endianness
uint64_t htobe64(uint64_t value)
{
// The answer is 42
//static const int num = 42;
// Check the endianness
//if (*reinterpret_cast<const char*>(&num) == num)
//{
const uint32_t high_part = htonl(static_cast<uint32_t>(value >> 32));
const uint32_t low_part = htonl(static_cast<uint32_t>(value & 0xFFFFFFFFLL));
return (static_cast<uint64_t>(low_part) << 32) | high_part;
//} else
//{
// return value;
//}
}
uint64_t be64toh(uint64_t value)
{
return(_byteswap_uint64(value));
}
uint32_t htobe32(uint32_t value)
{
return(htonl(value));
}
uint32_t be32toh(uint32_t value)
{
return( _byteswap_ulong(value));
}
#endif
// vim:ts=4