title: How to read/write unsigned byte array between C# and Java on a file?
body: <p>This question is a bit similar to <a href="https://stackoverflow.com/questions/22560406/how-to-write-numbers-to-a-file-and-make-them-readable-between-java-and-c-sharp">my previous one</a>, where I asked a "cross-language" way to write and read integers between a Java and a C# program. Problem was the endianess.</p>
  
  <p>Anyway, I'm facing a secondary problem. My goal is now to store and retrieve an array of unsigned bytes (values from 0 to 255) in a way that it can be processed by both Java and C#.</p>
  
  <p>In C# it's easy, since <code>unsigned byte[]</code> exists:</p>
  
  <pre><code>BinaryWriterBigEndian writer = new BinaryWriterBigEndian(fs);
  // ...
  writer.Write(byteData, 0, byteData.Length);
  </code></pre>
  
  <p><code>BinaryWriterBigEndian</code> is... well... a big-endian binary writer ;)</p>
  
  <p>This way, the file will contain a sequence composed by, for example, the following values (in a big-endian representation):</p>
  
  <pre><code>[0][120][50][250][221]...
  </code></pre>
  
  <p>Now it's time to do the same thing under Java. Since <code>unsigned byte[]</code> does not exist here, the array is stored in memory as a (signed) <code>int[]</code> in order to have the possibility to represent values higher than 127.
  How to write it as a sequence of <code>unsigned byte</code> values like C# does?</p>
  
  <p>I tried with this:</p>
  
  <pre><code>ByteBuffer byteBuffer = ByteBuffer.allocate(4 * dataLength);
  IntBuffer intBuffer = byteBuffer.asIntBuffer();
  intBuffer.put(intData);
  outputStream.write(byteBuffer.array());
  </code></pre>
  
  <p>Writing goes well, but C# is not able to read it in the proper way.</p>