I need a byte array with 4 bytes which contains 2 shorts. I have now this:
MemoryStream str = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(stream)) {
writer.Write((short) printscreen.Width);
writer.Write((short) printscreen.Height);
}
byte[] bytes = str.ToArray();
In java I used bytebuffer. But what should I use for c#?
I send this bytes through a socket to a java server. Java code:
byte[] data = new byte[in.available()];
in.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
System.out.println(buffer.getShort());
From the documentation on Java’s ByteBuffer Class:
From the documentation on .NET’s BinaryWriter.Write(short) Method:
Since you can’t change BinaryWriter, you need to change the byte order used by the ByteBuffer: