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

后端 未结 27 4290
花落未央
花落未央 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 05:09

    The Apache Commons Codec library has a Hex class for doing just this type of work.

    import org.apache.commons.codec.binary.Hex;
    
    String foo = "I am a string";
    byte[] bytes = foo.getBytes();
    System.out.println( Hex.encodeHexString( bytes ) );
    

提交回复
热议问题