【问题标题】:Java How to read unicode characters in socket? [duplicate]Java 如何读取套接字中的 unicode 字符? [复制]
【发布时间】: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 以求您的帮助!

【问题讨论】:

标签: java sockets unicode


【解决方案1】:

这取决于您输入的编码方案。如果您不想做繁重的工作,您可以使用 Apache IOUtils 并将字节转换为 unicode 字符串。 例子 : IOUtils.toString(bytes, "UTF-8")

【讨论】:

  • 我已经解决了我的问题:String s = new String(bytes, "UTF-8");我认为它与您的解决方案相似~thx 以及
猜你喜欢
  • 2012-12-02
  • 1970-01-01
  • 2012-12-17
  • 2021-09-16
  • 2018-11-18
  • 1970-01-01
  • 2013-10-12
  • 1970-01-01
相关资源
最近更新 更多