从一个字节中获取半个字节

Get half bytes from a byte

我有一个从一些字节生成的 SHA-512 字节数组。 484c1514b468429967aa0c8e2ab6d99c14e7cd4a45605cb834ebfd3612a0cc9184510d8d0a9c92e636d82c065fa2db0e05ef5c2518153a6c4ca9eebbe8d7b475

当我像下面这样迭代时;

for(byte x = 0; x < s512in.length; x++){
    System.out.println(String.format("%02X ", s512in[x]) + " );
}

我可以得到 48,4C,15.... 但我想从字节数组中得到 4,8,4,C...。我如何在不转换为字符串的情况下获得它?

使用如下。

    try {

        byte[] data = "nibbles".getBytes("UTF-8");
        for (int i = 0; i < data.length; i++) {
            String temp = Integer.toString((data[i] & 0xff) + 0x100, 16)
                    .substring(1);
            System.out.println(temp);
        }

    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
byte[] data = "nibbles".getBytes(StandardCharsets.US_ASCII);
for(byte b : data) {
    int high = (b & 0xf0) >> 4;
    int low = b & 0xf;
    System.out.format("%x%n", high);
    System.out.format("%x%n", low);
}