Download

Reading Written Binary Files from a different language and platform.

I am writing a binary reader that needs to read in a very specific set of binary written by an Android Phone. I have run into several issues reading this binary, the first and foremost being that it does not resemble the data I wrote in the first place. I have read a little bit about endianness, words, and how they are made on different systems and I am curious as to if this could be the root of the problem.
Any information would be good at this point, but the specific thing I would like to know is: Considering the lines below, why is the binary not being read in as the same value as it is written out as? How can I fix this?

Say numPoints = 5000.

(OUT FUNCTION - Android-java)
out.writeInt(numPoints);
(IN FUNCTION - UE4-c++)
reader << numPoints;

numPoints now equals some really really large number that I can’t explain.

I am using Windows 8.1 x64 and a Google-Project-Tango Tablet.

Found the answer via this stack overflow thread. Also, I have reason to believe thanks to this documentation that UE4 reads and writes in the endianness native to the platform it is running on. Windows 8.1 being little endian… and java writing in big endian is what caused the problem.

You can make a FArchive read in the opposite endian to the current platform using Ar.SetByteSwapping(), but you need to know the current platform endianness and the source asset endianness in order to determine whether or not swapping is necessary. Do you know if the Java file is guaranteed to always be big-endian? Unreal defines PLATFORM_LITTLE_ENDIAN to be 0 or 1 depending on your platform.

Cheers,
Michael Noland

JVM internal is strict big endian everywhere, but class libraries are able to output in native endianess if they are programmed to do so.