简体   繁体   中英

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

Connection.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.

通过new InputStreamReader(..,"utf-8")或您输入的任何内容在流上设置正确的编码。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM