Reading Special Characters (Java)

I make a chat client that uses special encryption. He has a problem reading letters like ", ƒ, ̕" from the input buffer.

I read them into an array of bytes and I tried to use

Connection.getInputStream().read();

And also using

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

But there is a problem because it displays them as square.

+3
source share
4 answers

You must make sure that your InputStreamReader uses the same encoding to decode bytes in characters than the one used by the sender to encode characters into bytes. Take a look at the other InputStreamReader constructors.

You should also make sure that the font you use to display characters supports your special characters.

+4

new InputStreamReader(..,"utf-8") .

+4

Convert a byte array to a string specifying a character set.

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

make sure the font mapping supports UTF-8 or the specified encoding.

0
source

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

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

  char c = in.readChar();

should do what you want.

-1
source

Source: https://habr.com/ru/post/1786523/


All Articles