You must know the endianness or the source data. Data is usually big-endian when being transferred over a network. Then you need to determine if your system is a little-endian or a big-endian machine. If the endianness of the data and the system is not the same, just reverse the bytes, and then use it.
You can determine the endianness of your system as follows:
int is_little_endian() {
short a = 1;
return *((char*)&a) & 1;
}
Convert from little/big endian to system endian and vice versa using these macros:
#define LITTLE_X_SYSTEM(dst_type, src) if(!is_little_endian()) memrev((src), 1 , sizeof(dst))
#define BIG_X_SYSTEM(dst_type, src) if(is_little_endian()) memrev((src), 1, sizeof(dst))
You can use it like this:
template <class T>
T readData(size_t position)
{
byte rawData[sizeof(T)] = { 0, };
// assuming source data is big endian
BIG_X_SYSTEM(T, rawData);
return *((T*)rawData);
}
This answer gives some more insight into endianness.