开发者

Trouble reading special characters (Java)

I'm making a chat client that uses special encryption. It has a problem reading letters like «, ƒ, ̕ from the input buffer.

Im reading them into a byte array and I tried using

Conne开发者_JAVA百科ction.getInputStream().read();

And also using

BufferedReader myInput = new BufferedReader(
    new InputStreamReader(Connection.getInputStream()));

But there appears to be a problem as it displays them as square boxes.


You have to make sure that your InputStreamReader uses the same charset to decode the bytes into chars than the one used by the sender to encode chars into bytes. Look at the other constructors of InputStreamReader.

You must also make sure that the font you're using to display the chars supports your special characters.


Set the correct encoding on the stream through new InputStreamReader(..,"utf-8") or whatever your input is.


Conver byte array to String specifying Character set.

String data = new String(byte[], "UTF-8");

make sure that displaying font support UTF-8 or your specified encoding charset.


You can try using a DataInputStream and the readChar() method.

  DataInputStream in = new DataInputStream(myinput);
  //where muinput is your BufferedInputStream.

  char c = in.readChar();

should do what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜