SharonKo
SharonKo

Reputation: 619

Socket input stream and Unicode

I try to write an app that sends text from Windows computer to Android cellphone. The text I send can be in English or Hebrew (for example). The connection is via Socket. The code I use on the Windows side (Visual studio 2012):

String buffer = // Some text
// Encode the data string into a byte array.            
byte[] msg = Encoding.ASCII.GetBytes(buffer + "\n");
// Send the data through the socket.
int bytesSent = socketSender.Send(msg);

And on the Android side:

//After I establish the Socket
String text = "";
InputStream is = socket.getInputStream();    
InputStreamReader isr = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isr);
while ((inputText = in.readLine()) != null)
{
     text = inputText;
}

All this works perfectly when sending English text. When I try to send Hebrew text I replace to this line:

byte[] msg = Encoding.Unicode.GetBytes(buffer + "\n");

But on the Android side I can't "read" it. I tried to use CharsetEncoder but didn't work (or I did it the wrong way). Any ideas?

Upvotes: 2

Views: 369

Answers (1)

SharonKo
SharonKo

Reputation: 619

Ok, so the answer is: on the Windows side:

byte[] msg = Encoding.UTF8.GetBytes(buffer + "\n");

And on the Android side:

InputStreamReader isr = new InputStreamReader(is, "UTF-8");

Upvotes: 2

Related Questions