How do I convert between big-endian and little-endian values in C++?
EDIT: For clarity, I have to translate binary data (double-precision floating point values and 3
If you have C++ 17 then add this header
#include
Use this template function to swap the bytes:
template
void swapEndian(T& buffer)
{
static_assert(std::is_pod::value, "swapEndian support POD type only");
char* startIndex = static_cast((void*)buffer.data());
char* endIndex = startIndex + sizeof(buffer);
std::reverse(startIndex, endIndex);
}
call it like:
swapEndian (stlContainer);