tags:

views:

215

answers:

5

I want to send BigInteger data in socket and my friend wants to retrieve the data.
I am using Java, but my friend uses C#.

String str = "Hello";
BigInteger big = new BigInteger(str.getBytes);
byteToBeSent[] = big.toByteArray();

I am sending this byte array (byteToBeSent[]) through socket. And my friend wants to retrieve "Hello".

How is this possible?

+2  A: 

Honestly, your best bet would to be use the built in Encoding classes in C#.

string str = "Hello";
byte[] data = System.Text.Encoding.UTF8.GetBytes(str);

And then send that through the socket.

Navaar
A: 

You'll need to get a custom BigInteger class for C#.

But parsing "Hello" as a biginteger isn't going to work. I you want to send text, you're better of using Navaars method.

Carra
+1  A: 

Why do you use BigInteger to send String data? Or is that just an example?

If you want to send String data, use String.getBytes(String encoding), send the result and decode it using System.Text.Encoding.

Michael Borgwardt
+3  A: 

From Java, send your string using String.getBytes(encoding) and specify the encoding to match how your friend will read it (e.g. UTF-8).

This will translate your string into a byte stream that will be translatable at the C# end due to the fact that you're both agreeing on the encoding mechanism.

I'm not sure what your BigInteger mechanism is doing, but I don't believe it'll be portable, nor handle sizable strings.

Brian Agnew
A: 

On the C# end, you can use System.Text.Encoding.ASCII.GetString(bytesToConvert[]) to convert the received byte array back to a string.

I had thought that was some sort of Java idiom to convert a string to a byte array. It does correctly convert the string into ASCII bytes, and since BigInteger length is arbitrary, the length of the string should not be an issue.

R Ubben