美文网首页
Java字符串编码

Java字符串编码

作者: 风亡小窝 | 来源:发表于2017-01-15 15:26 被阅读111次

    Java中的字符串都是unicode编码。

    // GBK编码中 “我” 对应的字节序列
    byte[] s1 = "我".getBytes("gbk");
    
    // unicode编码中 “我” 对应的字节序列
    byte[] s2 = "我".getBytes("utf8");
    
    // 将字节序列 s1 以 GBK编码 进行解码
    System.out.println(new String(s1, "GBK"));
    
    //将字节序列 s1 以 unicode编码 进行解码,出现乱码
    System.out.println(new String(s1, "utf8"));
    

    "我".getbytes("GBK") 则是将 unicode中的“我”转换为了 GBK中的 “我”

    Paste_Image.png

    相关文章

      网友评论

          本文标题:Java字符串编码

          本文链接:https://www.haomeiwen.com/subject/tenybttx.html