Difference between C# and java big endian bytes using miscutil
        Posted  
        
            by Eric Hauser
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Eric Hauser
        
        
        
        Published on 2010-04-08T05:12:59Z
        Indexed on 
            2010/04/08
            5:13 UTC
        
        
        Read the original article
        Hit count: 477
        
I'm using the miscutil library to communicate between and Java and C# application using a socket. I am trying to figure out the difference between the following code (this is Groovy, but the Java result is the same):
import java.io.*
def baos = new ByteArrayOutputStream();
def stream = new DataOutputStream(baos);
stream.writeInt(5000)
baos.toByteArray().each { println it }
/* outputs - 0, 0, 19, -120 */
and C#:
using (var ms = new MemoryStream())
using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big, ms, Encoding.UTF8))
{
  writer.Write(5000);
  ms.Position = 0;
  foreach (byte bb in ms.ToArray())
  {
    Console.WriteLine(bb);
  }
}
/* outputs - 0, 0, 19, 136 */
As you can see, the last byte is -120 in the Java version and 136 in C#.
© Stack Overflow or respective owner