Oct 13

You have a couple of choices, either:

  • Loop thru the array to convert each byte individually, or
  • Use the toString() method of the BigInteger class

// using a loop

StringBuilder sb = 
   new StringBuilder(bytes.length * 2);
for (int i=0; i< bytes.length; i++)
    sb.append(String.format("%02x", bytes[i]));
String hex1 = sb.toString();

// using BigInteger

BigInteger bi = new BigInteger(bytes);
String hex2 = bi.toString(16); 

If you are dealing with large byte arrays or performance is an issue then you should have a read of “Converting large byte array to binary string”.

written by objects \\ tags: , , , ,

Leave a Reply

You must be logged in to post a comment.