How to convert a byte array to a hex string in Java?

后端 未结 27 4277
花落未央
花落未央 2020-11-21 04:19

I have a byte array filled with hex numbers and printing it the easy way is pretty pointless because there are many unprintable elements. What I need is the exact hexcode in

27条回答
  •  轮回少年
    2020-11-21 04:55

    A Guava solution, for completeness:

    import com.google.common.io.BaseEncoding;
    ...
    byte[] bytes = "Hello world".getBytes(StandardCharsets.UTF_8);
    final String hex = BaseEncoding.base16().lowerCase().encode(bytes);
    

    Now hex is "48656c6c6f20776f726c64".

提交回复
热议问题