HexUtil { public static String toHexString(byte[] bytes) { if (bytes == null) { return ""; } return toHexString(bytes, 0, bytes.length); }  static String toHexString(byte[] bytes); static String toHexString(byte[] bytes, int offset, int length); static void appendHexString(StringBuilder buffer, byte[] bytes); static void appendHexString(StringBuilder buffer, byte[] bytes, int offset, int length); static String toHexString(byte value); static void appendHexString(StringBuilder buffer, byte value); static String toHexString(short value); static void appendHexString(StringBuilder buffer, short value); static String toHexString(int value); static void appendHexString(StringBuilder buffer, int value); static String toHexString(long value); static void appendHexString(StringBuilder buffer, long value); static int hexCharToIntValue(char c); static byte[] toByteArray(CharSequence hexString); static byte[] toByteArray(CharSequence hexString, int offset, int length); static char[] HEX_TABLE; }
@Test public void toHexStringWithByteArray() throws Exception { String s0 = null; s0 = HexUtil.toHexString(null); Assert.assertEquals("", s0); s0 = HexUtil.toHexString(new byte[0]); Assert.assertEquals("", s0); s0 = HexUtil.toHexString(new byte[] { 0x34 }); Assert.assertEquals("34", s0); s0 = HexUtil.toHexString(new byte[] { 0x34, 0x35 }); Assert.assertEquals("3435", s0); s0 = HexUtil.toHexString(new byte[] { (byte)0xFF, (byte)0xAB }); Assert.assertEquals("FFAB", s0); }
@Test public void toHexStringWithByteArrayAndOffsetLength() throws Exception { String s0 = null; s0 = HexUtil.toHexString(null, 0, 0); Assert.assertEquals("", s0); s0 = HexUtil.toHexString(new byte[0], 0, 0); Assert.assertEquals("", s0); s0 = HexUtil.toHexString(new byte[] { 0x34 }, 0, 1); Assert.assertEquals("34", s0); s0 = HexUtil.toHexString(new byte[] { 0x34, 0x35 }, 0, 2); Assert.assertEquals("3435", s0); s0 = HexUtil.toHexString(new byte[] { (byte)0xFF, (byte)0xAB }, 0, 2); Assert.assertEquals("FFAB", s0); s0 = HexUtil.toHexString(new byte[] { (byte)0xFF, (byte)0xAB }, 1, 1); Assert.assertEquals("AB", s0); try { HexUtil.toHexString(new byte[0], -1, 0); Assert.fail(); } catch (IllegalArgumentException e) { } try { HexUtil.toHexString(new byte[0], 0, -1); Assert.fail(); } catch (IllegalArgumentException e) { } try { HexUtil.toHexString(new byte[0], 1, 0); Assert.fail(); } catch (ArrayIndexOutOfBoundsException e) { } }
@Test public void toHexStringWithByte() throws Exception { String s0 = null; s0 = HexUtil.toHexString((byte)0x65); Assert.assertEquals("65", s0); s0 = HexUtil.toHexString((byte)0x34); Assert.assertEquals("34", s0); s0 = HexUtil.toHexString((byte)0x00); Assert.assertEquals("00", s0); s0 = HexUtil.toHexString((byte)-1); Assert.assertEquals("FF", s0); }
@Test public void toHexStringWithShort() throws Exception { String s0 = null; s0 = HexUtil.toHexString((short)0x65); Assert.assertEquals("0065", s0.toString()); s0 = HexUtil.toHexString((short)0x6534); Assert.assertEquals("6534", s0.toString()); s0 = HexUtil.toHexString((short)0x00); Assert.assertEquals("0000", s0.toString()); s0 = HexUtil.toHexString((short)-1); Assert.assertEquals("FFFF", s0.toString()); }
@Test public void toHexStringWithInt() throws Exception { String s0 = null; s0 = HexUtil.toHexString((int)0x65); Assert.assertEquals("00000065", s0.toString()); s0 = HexUtil.toHexString((int)0x6534); Assert.assertEquals("00006534", s0.toString()); s0 = HexUtil.toHexString((int)0x00); Assert.assertEquals("00000000", s0.toString()); s0 = HexUtil.toHexString(-34000); Assert.assertEquals("FFFF7B30", s0.toString()); s0 = HexUtil.toHexString(-1); Assert.assertEquals("FFFFFFFF", s0.toString()); }
@Test public void toHexStringWithLong() throws Exception { String s0 = null; s0 = HexUtil.toHexString(0x65L); Assert.assertEquals("0000000000000065", s0.toString()); s0 = HexUtil.toHexString(0x6534L); Assert.assertEquals("0000000000006534", s0.toString()); s0 = HexUtil.toHexString(0x00L); Assert.assertEquals("0000000000000000", s0.toString()); s0 = HexUtil.toHexString(-34000L); Assert.assertEquals("FFFFFFFFFFFF7B30", s0.toString()); s0 = HexUtil.toHexString(-1L); Assert.assertEquals("FFFFFFFFFFFFFFFF", s0.toString()); }