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

Java 8Object Oriented ProgrammingProgramming

The printHexBinary() method of the DatatypeConverter class accepts a byte array and returns a hex string.

Example

import javax.xml.bind.DatatypeConverter;
public class ByteToHexString {
   public static void main(String args[]) {
      String sam = "Hello how are you how do you do";
      byte[] byteArray = sam.getBytes();
      String hex = DatatypeConverter.printHexBinary(byteArray);
      System.out.println(hex);
  }
}

Output

48656C6C6F20686F772061726520796F7520686F7720646F20796F7520646F
raja
Published on 09-Jan-2018 16:53:46
Advertisements