【发布时间】:2013-11-04 09:25:07
【问题描述】:
服务器接收字节数组作为输入流,我用DataInputStream封装了流。前2个字节表示字节数组的长度,后2个字节表示一个标志,接下来的字节由内容组成。我的问题是内容包含有 2 个字节的 unicode 字符。我怎样才能读取 unicode char?我的上一个代码是:
DataInputStream dis = new DataInputStream(is);
int length = dis.readUnsignedShort();
int flag = dis.readUnsignedShort();
String content = "";
int c;
for (int i = 0; i < length - 4; i++) {
c = dis.read();
content += (char) c;
}
它只能读取 ascII.thxs 以求您的帮助!
【问题讨论】:
-
在此处查看 BalusC 的答案:stackoverflow.com/questions/4505057/datainputstream-and-utf-8
-
如果这些代码点是大端 UTF-16,另请参见 readChar 方法。
-
非常感谢。答案解决了我的问题。