Java code To convert byte to Hexadecimal

后端 未结 19 2446
我寻月下人不归
我寻月下人不归 2020-11-22 17:26

I have an array of bytes. I want each byte String of that array to be converted to its corresponding hexadecimal values.

Is there any function in Java to convert a b

19条回答
  •  一整个雨季
    2020-11-22 17:51

    If you want a constant-width hex representation, i.e. 0A instead of A, so that you can recover the bytes unambiguously, try format():

    StringBuilder result = new StringBuilder();
    for (byte bb : byteArray) {
        result.append(String.format("%02X", bb));
    }
    return result.toString();
    

提交回复
热议问题