StringHelper { public static String bytesToHexString(byte[] bytes) { if(bytes == null) { return null; } final char[] hexChars = new char[bytes.length * 2]; int v; for ( int j = 0; j < bytes.length; j++ ) { v = bytes[j] & 0xFF; hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } return new String(hexChars); }  static String bytesToHexString(byte[] bytes); static byte[] hexStringToByteArray(String s);  }
@Test public void testBytesToHexString() { assertEquals(null, StringHelper.bytesToHexString (null)); assertEquals("", StringHelper.bytesToHexString(new byte[] {})); assertEquals("01020304", StringHelper.bytesToHexString(new byte[] {1, 2, 3, 4})); assertEquals("0E0F10", StringHelper.bytesToHexString(new byte[] {14, 15, 16})); assertEquals("80FF", StringHelper.bytesToHexString(new byte[] {-128, -1})); }