There is no standard function to detect the validity of your syatem. However, given a function bool is_little_endian()that returns true only on small systems, you can do something like this:
std::uint32_t read_from_little_endian(char* buf)
{
std::uint32_t u;
if(is_little_endian())
std::copy(buf, buf + sizeof(u), (char*)&u);
else
std::reverse_copy(buf, buf + sizeof(u), (char*)&u);
return u;
}
The important point is always to bring std::uint32_t*to char*, because only char*can legally aliases of all the other types.
source
share