4

When sending information from a java application to a C# application through sockets, is the byte order different? Or can I just send an integer from C# to a java application and read it as integer?

(And do the OS matter, or is the same for java/.net no matter how the actual OS handles it?)

Robin Green
  • 32,079
  • 16
  • 104
  • 187
jgauffin
  • 99,844
  • 45
  • 235
  • 372

4 Answers4

8

It all comes down to how you encode the data. If you are treating it only as a raw sequence of bytes, there is no conflict; the sequence is the same. When the matters is in endianness when interpreting chunks of the data as (for example) integers.

Any serializer written with portability in mind will have defined endianness - for example, in protocol buffers (available for both Java and C#) little-endian is always used regardless of your local hardware.

If you are doing manual writing to the stream, using things like shift-based encoding (rather than direct memory copying) will give you defined endianness.

If you use pre-canned platform serializers, you are at the mercy of the implementation. It might be endian-safe, it might not be (i.e. it might depend on the platform at both ends). For example, the .NET BitConverter class is not safe - it is usually assumed (incorrectly) to be little-endian, but on some platforms (and particularly in Mono on some hardware) it could be big-endian; hence the .IsLittleEndian property.

My advice would be to use a serializer that handles it all for you ;p

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

In Java, you can use a DataInputStream or DataOutputStream which read and write the high-byte first, as documented:

http://download.oracle.com/javase/6/docs/api/java/io/DataOutputStream.html#writeInt%28int%29

You should check corresponding C# documentation to see what it does (or maybe someone here can tell you).

You also have, in Java, the option of using ByteByffer:

http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html

... which has the "order" method to allow you to specify a byte order for operations reading multi-byte primitive types.

davmac
  • 20,150
  • 1
  • 40
  • 68
3

Java uses Big Endian for some libraries like DataInput/OutputStream. IP protocols all use Big Endian which can lead people to use Big Endian as default for network protocols.

However NIO, ByteBuffer allows you to specify BigEndian, LittleEndian or NativeEndian (whatever the system uses by default)

x86 systems tend to use little endian and so many Microsoft/Linux applications use little endian by default but can support big-endian.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Yes, the byte order may be different. C# assumes little-endian may use the platform's byte ordering, Java tends to use big-endian. This has been discussed before on SO. See for example C# little endian or big endian?

Community
  • 1
  • 1
Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • 1
    C# makes no such assumption; there are some byte-level utilities (`BitConverter` etc) , but they use the platform endianness, which can be different on some platforms – Marc Gravell May 14 '11 at 08:52