views:

1090

answers:

1

I was writing an article in which I wanted to show how to send objects across the network, from Java to CLR/.Net and Back.

http://ferozedaud.blogspot.com/2009/11/howto-serialize-data-from-object-from.html

While doing research for this, I could not find a Java equivalent for BitConverter class that exists in .NET. Due to this, I had to resort to using ByteBuffer to marshal primitive types to byte representation and vice versa.

Does anyone know of a Java equivalent for BitConverter class?

+3  A: 

The nearest equivalents are DataInput/DataOutput (and DataInputStream, DataOutputStream etc).

In fact, those are more like BinaryReader/BinaryWriter, but they're probably what you want. I don't think there's anything directly equivalent to BitConverter, although you could fairly easily write your own code if necessary.

Jon Skeet
Thanks, Jon. I tried those. However the DataOutputStream seems to format the data differently. It is not just writing out an Int in some Endian order. It is writing out something else entirely.
feroze
@feroze: That seems unlikely. If you were using writeInt, it will have written four bytes out. This is a different endianness to BitConverter, but it should still be okay. If that's not what you were seeing, please edit your question with a sample program demonstrating the problem.
Jon Skeet
You are right. DataOutputStream writes data in BigEndian format. While BitConverter does not support BigEndian, I could get my sample to work by using the IPAddress.NetworkToHostOrder() and IPAddress.HostToNetworkOrder() methods.Thanks for your help!
feroze
@feroze: I have a variable-endian version of BitConverter in my MiscUtil project: http://pobox.com/~skeet/csharp/miscutil
Jon Skeet