- added new byte order code

This commit is contained in:
Mark Vejvoda
2012-11-01 00:19:46 +00:00
parent 3303d382e3
commit b6b1abd43e
2 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
#ifndef BYTE_ORDER_H
#define BYTE_ORDER_H
#include <algorithm>
namespace Shared{ namespace PlatformByteOrder {
template<class T> T EndianReverse(T t) {
unsigned char uc[sizeof t];
memcpy(uc, &t, sizeof t);
for (unsigned char *b = uc, *e = uc + sizeof(T) - 1; b < e; ++b, --e) {
std::swap(*b, *e);
}
memcpy(&t, uc, sizeof t);
return t;
}
static bool isBigEndian() {
short n = 0x1;
return (*(char*)(&n) == 0x0);
}
template<class T> T toCommonEndian(T t) {
static bool bigEndianSystem = isBigEndian();
if(bigEndianSystem == true) {
t = EndianReverse(t);
}
return t;
}
template<class T> T fromCommonEndian(T t) {
static bool bigEndianSystem = isBigEndian();
if(bigEndianSystem == true) {
t = EndianReverse(t);
}
return t;
}
}}
#endif