读取特殊字符时遇到问题 (Java)
我正在制作一个使用特殊加密的聊天客户端。它在从输入缓冲区读取诸如 «、f、̕ 之类的字母时遇到问题。
我将它们读入字节数组,并尝试使用
Connection.getInputStream().read();
And 还使用
BufferedReader myInput = new BufferedReader(
new InputStreamReader(Connection.getInputStream()));
但似乎存在问题,因为它将它们显示为方框。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须确保您的 InputStreamReader 使用相同的字符集将字节解码为字符,而不是发送者将字符编码为字节所使用的字符集。查看InputStreamReader的其他构造函数。
您还必须确保用于显示字符的字体支持特殊字符。
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") 或任何您的输入在流上设置正确的编码。
Set the correct encoding on the stream through
new InputStreamReader(..,"utf-8")
or whatever your input is.将字节数组转换为指定字符集的字符串。
确保显示字体支持 UTF-8 或您指定的编码字符集。
Conver byte array to String specifying Character set.
make sure that displaying font support UTF-8 or your specified encoding charset.
您可以尝试使用 DataInputStream< /a> 和 readChar() 方法。
应该做你想做的事。
You can try using a DataInputStream and the readChar() method.
should do what you want.