search

Home  >  Q&A  >  body text

java - About string encoding.

public static void main(String[] args) throws Exception {
    String str = "resource";
    System.out.println(Arrays.toString(getHash(str,"MD5").getBytes()));//使用默认解码后输出
}

public static String getHash(String str, String hashType) {
    try {
        MessageDigest digest = MessageDigest.getInstance(hashType);
        digest.reset();
        byte[] b = digest.digest(str.getBytes());
        System.out.println(Arrays.toString(b)); //编码前输出
        return new String(b); //使用默认编码
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return str;
}

Output:
[-106, -85, 78, 22, 63, 78, -32, 58, -86, 77, 16, 81, -86, 81, -46, 4]

[-17, -65, -67, -17, -65, -67, 78, 22, 63, 78, -17, -65, -67, 58, -17, -65, -67, 77, 16, 81, -17, -65, -67, 81, -17, -65, -67, 4]

Why are the outputs before encoding and decoding after encoding different?

我想大声告诉你我想大声告诉你2746 days ago681

reply all(1)I'll reply

  • 黄舟

    黄舟2017-05-27 17:43:40

    You might think(new String(b)).getBytes().equals(b),实际上并非如此。(尽管new String(s.getBytes()).equals(s)it must be. )

    Becausebyte[]转换成String时,有些字节是未必能转换成字符的,比如第一个-106、第二个-85就是,所以转换成String时前两个就变成了未知字符(表面上会显示?,但实际上是一个Unicode字符),再转成byte[](你这边defaultCharset应该是UTF-8), each unknown character becomes 3 bytes.

    If you use GBK, the situation is not bad, but still slightly different:

    [-106, -85, 78, 22, 63, 78, -32, 58, -86, 77, 16, 81, -86, 81, -46, 4]
    [-106, -85, 78, 22, 63, 78, 63, 58, -86, 77, 16, 81, -86, 81, 63, 4]
    

    So the conclusion is: If you use String to represent a Hash value, you cannot force-convert byte[] to String, but convert it to hexadecimal representation as usual.

    reply
    0
  • Cancelreply