Friday, November 7, 2008

Reverse endianness in C++

Here you go kids:

///\brief reverses the endianness of a string
template <typename T> inline
void reverse_endian(T& t){
unsigned char* res = reinterpret_cast<unsigned char*>(&t);
unsigned char *temp = new unsigned char[sizeof(T)];
for(int n=0;n<sizeof(T);n++) {temp[sizeof(T)-1-n] = res[n]; }
for(int n=0;n<sizeof(T);n++) res[n] = temp[n];
delete[] temp;
}



Have fun yall!

Update: now with added working!

No comments: