Convert { public static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for ( int j = 0; j < bytes.length; j++ ) { int v = bytes[j] & 0xFF; hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } return new String(hexChars); }  static String bytesToHex(byte[] bytes); static byte[] strToBytes(String str); static byte[] longToBytes(long val); static boolean getNthBit(byte[] arr, int offset); static byte[] getPrefixBytes(byte[] arr);  }
@Test public void testBytesToHex() { byte[] rand = new byte[4]; new Random().nextBytes(rand); String conv = Convert.bytesToHex(rand); assertTrue("Bad length of hex string", conv.length() == 8); byte[] b = new byte[4]; try { b[0] = (byte)Integer.parseInt(conv.substring(0, 2), 16); b[1] = (byte)Integer.parseInt(conv.substring(2, 4), 16); b[2] = (byte)Integer.parseInt(conv.substring(4, 6), 16); b[3] = (byte)Integer.parseInt(conv.substring(6, 8), 16); } catch (NumberFormatException e) { fail("Conversion of bytes to hex string failed - "+e.getMessage()); } assertArrayEquals("Conversion of bytes to hex string failed", rand, b); }