If two machines have different endianness, then there is no optimal byte order for either one. Therefore, this problem can't be solved in a one-size-fits-all optimal solution. People have tried to make code generators to take some of the pain out of encoding/decoding but that isn't effortless either.