title: How to write numbers to a file and make them readable between Java and C#
body: <p>I'm into a "compatibility" issue between two versions of the same program, the first one written in Java, the second it's a port in C#.</p>
  
  <p>My goal is to write some data to a file (for example, in Java), like a sequence of numbers, then to have the ability to read it in C#. Obviously, the operation should work in the reversed order.</p>
  
  <p>For example, I want to write 3 numbers in sequence, represented with the following schema:</p>
  
  <ul>
  <li>first number as one 'byte' (4 bit)</li>
  <li>second number as one 'integer' (32 bit)</li>
  <li>third number as one 'integer' (32 bit)</li>
  </ul>
  
  <p>So, I can put on a new file the following sequence: 2 (as byte), 120 (as int32), 180 (as int32)</p>
  
  <p>In Java, the writing procedure is more or less this one:</p>
  
  <pre><code>FileOutputStream outputStream;
  byte[] byteToWrite;
  // ... initialization....
  
  // first byte
  outputStream.write(first_byte);
  
  // integers
  byteToWrite = ByteBuffer.allocate(4).putInt(first_integer).array();
  outputStream.write(byteToWrite);
  byteToWrite = ByteBuffer.allocate(4).putInt(second_integer).array();
  outputStream.write(byteToWrite);
  
  outputStream.close();
  </code></pre>
  
  <p>While the reading part it's the following:</p>
  
  <pre><code>FileInputStream inputStream;
  ByteBuffer byteToRead;
  // ... initialization....
  
  // first byte
  first_byte = inputStream.read();
  
  // integers
  byteToRead = ByteBuffer.allocate(4);
  inputStream.read(byteToRead.array());
  first_integer = byteToRead.getInt();
  
  byteToRead = ByteBuffer.allocate(4);
  inputStream.read(byteToRead.array());
  second_integer = byteToRead.getInt();
  
  inputStream.close();
  </code></pre>
  
  <p>C# code is the following. Writing:</p>
  
  <pre><code>FileStream fs;
  byte[] byteToWrite;
  // ... initialization....
  
  // first byte
  byteToWrite = new byte[1];
  byteToWrite[0] = first_byte;
  fs.Write(byteToWrite, 0, byteToWrite.Length);
  
  // integers
  byteToWrite = BitConverter.GetBytes(first_integer);
  fs.Write(byteToWrite, 0, byteToWrite.Length);
  byteToWrite = BitConverter.GetBytes(second_integer);
  fs.Write(byteToWrite, 0, byteToWrite.Length);
  </code></pre>
  
  <p>Reading:</p>
  
  <pre><code>FileStream fs;
  byte[] byteToWrite;
  // ... initialization....
  
  // first byte
  byte[] firstByteBuff = new byte[1];
  fs.Read(firstByteBuff, 0, firstByteBuff.Length);
  first_byte = firstByteBuff[0];
  
  // integers
  byteToRead = new byte[4 * 2];
  fs.Read(byteToRead, 0, byteToRead.Length);
  first_integer = BitConverter.ToInt32(byteToRead, 0);
  second_integer = BitConverter.ToInt32(byteToRead, 4);
  </code></pre>
  
  <p>Please note that both the procedures works when the same Java/C# version of the program writes and reads the file. The problem is when I try to read a file written by the Java program from the C# version and viceversa. Readed integers are always "strange" numbers (like -1451020...).</p>
  
  <p>There's surely a compatibility issue regarding the way Java stores and reads 32bit integer values (always <code>signed</code>, right?), in contrast to C#. How to handle this?</p>