cannot send serialized class from Android to PC

i am using unity 2020.3.22f1 LTS, i am sending data from one device to another via TCP, serializing class using binary formatter, it works from PC to PC but when i send data from andoid device to PC it gives me this error
SerializationException: Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.

I suspect endianess. BinaryFormatter, which you should refrain from using by all means anyway, serializes the bytes in the format of the native CPU.

Android is typically big endian, PCs (Windows) are typically using little endian. So a 4-byte integer with value one will be expressed as 0x00000001 in one system but as 0x01000000 in the other. Thus the two binary formats are incompatible.

I thought that Unity's Serialization package had that covered but couldn't find this noted in the docs. Give it a quick test, you only need to serialize a single non-zero int on both platforms to see if the resulting binary data is the same.

Compare the two formats byte by byte, at least the first couple bytes. They should not differ but if they do, check if the serialization code on either platform does something different, like serializing fields in a different order or skipping platform-specific fields.

Looks like you got bad info somewhere
[ CPU instruction set (and,registers when it calls functions](,registers%20when%20it%20calls%20functions).
Tons of Unity tech assumes little-endian as well :p or more precisely, a single shared endianness across platforms

1 Like