Mega Code Archive

 
Categories / Java / Data Type
 

Parsing and Formatting a Byte Array into Hexadecimal

import java.math.BigInteger; public class Main {   public static void main(String[] argv) throws Exception {     byte[] bytes = new byte[] { (byte) 0x12, (byte) 0x0F, (byte) 0xF0 };     BigInteger bi = new BigInteger(bytes);     // Format to hexadecimal     String s = bi.toString(16);      if (s.length() % 2 != 0) {       s = "0" + s;     }   } }