How can I use C # to fetch Java data from a socket?

I want to send data BigInteger

to a socket and my friend wants to receive data.
I am using Java, but my friend is using C #.

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

      

I am sending this byte array ( byteToBeSent[]

) over a socket. And my friend wants to receive "Hello".

How is this possible?

0


a source to share


5 answers


From Java, send your string using String.getBytes (encoding) and provide an encoding that matches how your friend will read it (like UTF-8).

This will translate your string into a stream of bytes, which will translate to the end of C # due to the fact that you both agree on the encoding mechanism.



I'm not sure what your BigInteger mechanism is doing, but I don't believe it will be portable and doesn't handle meaningful strings.

+3


a source


Honestly, it's best to use the built-in coding classes in C #.

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

      



And then send that over the socket.

+2


a source


Why are you using BigInteger

String to send data? Or is this just an example?

If you want to send String data, use String.getBytes(String encoding)

, send the result and decode it with System.Text.Encoding

.

+1


a source


You will need a custom BigInteger class for C # .

But parsing "Hello" as a big integrator won't work. I want to send text, it is better to use Navaars method.

0


a source


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

I thought it was some Java idiot for converting a string to a byte array. It will correctly convert the string to ASCII bytes, and since the length of the BigInteger is arbitrary, the length of the string shouldn't be an issue.

0


a source







All Articles