0

I want to send String from C# to Jave I do it in C# in 2 side like this

Sender: C#

NetworkStream ns1 = _client2.GetStream();
byte[] b = { (byte)2 };//this is the command that sent from client to Server  
ns1.Write(b, 0, 1);
string data = "222222222";
byte[] b2 = _AC.GetBytes(data);
ns1.Write(b2, 0, b2.Length);

Reciever C#

 Byte[] b = new byte[10];
 ns.Read(b, 0, b.Length);
 string Data = _AE.GetString(b);

 while (ns.DataAvailable)
   {
     b = new byte[10];
     ns.Read(b, 0, b.Length);
     Data += _AE.GetString(b);
    }

I do some coding with no luck in Java .... like this..

byte b = is.readByte();
byte[] buff= new byte[8];
is.read(buff,0,10);

I receive inside buff[50,50,50,50,50,50,50,50,50]; .....then how to change it to string in java ..... Any help would be great ....thanks

Anas ZO KO
  • 15
  • 1
  • 3

3 Answers3

2

You should specify the encoding of your string! If the c#-program runs on another machine with another operating system than the java-application you might get problems. The java-doc of the String(byte[])-Constructor says:

Constructs a new String by decoding the specified array of bytes using the platform's default charset. ...

Let's say you specify utf-8 string encoding. You should also make sure that c# is also sending an utf-8-string:

byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(myString);

on java-side you should read the bytes as an utf-8-string like this:

String str = new String(buff, "UTF-8");
thomas
  • 5,637
  • 2
  • 24
  • 35
1

String has a contsructor for bytes. Just use:

String myNewString = String(buff);

There are several constructors for Strings from bytes, if you don't have any offset, or length concerns then just use the constructor above. If you start to have issues with character sets you can use something like:

String myNewString = null;
try {
    myNewString = String(buff , "UTF-8") 
} catch (java.io.UnsupportedEncodingException uee) {
    //Using an unknown charset will get you here.
    uee.printStackTrace();
}
GavinCattell
  • 3,863
  • 20
  • 22
  • @Anas ZO KO - you're absolutely on the right track. To send a string over a socket between C# and Java, you should use byte buffers on both sides, to send and receive. Then all you need to do is, as GavinCattell said, convert byte[] buffer into a string with the right "String()" constructor. Voila - it's as easy as that :) – paulsm4 Mar 24 '12 at 20:27
  • PS: You'd do pretty much exacty the same thing if you wanted to send the string from Java to C#. – paulsm4 Mar 24 '12 at 20:28
0

50 is the numeric ASCII value of the character "2". Just take each element of buff and apply a cast of char to it: (char) buff[0], (char) buff[1] etc.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
  • 1
    But the LAST thing you want to do is read input byte by byte, if you can avoid it. Read the smallest subset of data you can all at once, *then* convert, and *then* process. Doing as much I/O at a time is a huge performance win. ALSO: as Thomas suggested, you might also need to specify the encoding type. For example, both C# and Java default to UTF-16. But Java UTF-16 is big-endian, and C# UTF-16 is little endian (completely different!): http://stackoverflow.com/questions/4793387/utf-16-encoding-in-java-versus-c-sharp – paulsm4 Mar 24 '12 at 21:49
  • I actually think that thomas's answer is the best. Weird to say but... I was kind of shocked you accepted my answer... – Radu Murzea Mar 25 '12 at 08:53