I’m writing a short to a file, using the following code in Java:
RandomAccessFile file = new RandomAccessFile("C:\\Users\\PC\\Desktop\\myFile.bin", "rw");
file.writeShort(11734);
file.close();
When I read it back in Java, I get the same (11734) number back. However, when I read the number in C# using the following code:
string p = "C:\\Users\\PC\\Desktop\\myFile.bin";
short s = new BinaryReader(File.OpenRead(p)).ReadInt16();
The variable s contains -10707.
How can this happen, and is there a way to retrieve the number I wrote to a file in Java, in C#?
BigEndian/LittleEndian problem. see below
s will be -10707
You can use
IPAddress.HostToNetworkOrderto convert from one form to another.You can also crete your own BinaryReader