yanshenxian
V2EX  ›  Java

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

  •  
  •   yanshenxian · May 11, 2020 · 1397 views
    This topic created in 2192 days ago, the information mentioned may be changed or developed.

    代码如下

    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
       May 12, 2020
    自答: 在 Stack Overflow 上找到答案了
    is-there-a-way-to-recover-a-byte-array-that-represents-a-string-that-is-saved-a
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3229 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 12:51 · PVG 20:51 · LAX 05:51 · JFK 08:51
    ♥ Do have faith in what you're doing.