0

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?

1
  • Creating a BigInteger from a string like "Hello" seems like a bad idea. Commented May 20, 2009 at 15:10

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.