V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yanshenxian
V2EX  ›  Java

Java 字节数组通过 UTF-8 编码成字符串后,无法恢复到原始字节数组

  •  
  •   yanshenxian · 2020-05-11 23:10:31 +08:00 · 953 次点击
    这是一个创建于 1417 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下

    byte[] bytes = new byte[] {39, 10, -111, -111, 54, 68, -2, 89, 68, 20, -92, 73, -83, -11, -5, -54, -122, -127, 97, 64};
    String stringValue = new String(bytes, StandardCharsets.UTF_8);
    byte[] newBytes1 = stringValue.getBytes(StandardCharsets.UTF_8);
    byte[] newBytes2 = stringValue.getBytes(StandardCharsets.ISO_8859_1);
    // 输出 false
    System.out.println(Arrays.equals(bytes, newBytes1));
    // 输出 false
    System.out.println(Arrays.equals(bytes, newBytes2));
    

    我要怎么将一个 UTF-8 编码的字符串恢复成原始字节数组呢

    yanshenxian
        1
    yanshenxian  
    OP
       2020-05-12 11:51:08 +08:00
    自答: 在 Stack Overflow 上找到答案了
    is-there-a-way-to-recover-a-byte-array-that-represents-a-string-that-is-saved-a
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3352 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:43 · PVG 18:43 · LAX 03:43 · JFK 06:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.