/** * Creates a String from a byte array with each byte in a "Big Endian" * hexidecimal format. For example, a byte 0x34 will return a * String "34". A byte array of { 0x34, 035 } would return "3435". * @param buffer The StringBuilder the byte array in hexidecimal format * will be appended to. If the buffer is null, this method will throw * a NullPointerException. * @param bytes The byte array that will be converted to a hexidecimal String. * If the byte array is null, this method will append nothing (a noop) * @param offset The offset in the byte array to start from. If the offset * or length combination is invalid, this method will throw an IllegalArgumentException. * @param length The length (from the offset) to conver the bytes. If the offset * or length combination is invalid, this method will throw an IllegalArgumentException. */ public static String toHexString(byte[] bytes, int offset, int length) { if (bytes == null) { return ""; } assertOffsetLengthValid(offset, length, bytes.length); // each byte is 2 chars in string StringBuilder buffer = new StringBuilder(length * 2); appendHexString(buffer, bytes, offset, length); return buffer.toString(); }
/** * Creates a String from a byte array with each byte in a "Big Endian" * hexidecimal format. For example, a byte 0x34 will return a * String "34". A byte array of { 0x34, 035 } would return "3435". * @param buffer The StringBuilder the byte array in hexidecimal format * will be appended to. If the buffer is null, this method will throw * a NullPointerException. * @param bytes The byte array that will be converted to a hexidecimal String. * If the byte array is null, this method will append nothing (a noop) * @param offset The offset in the byte array to start from. If the offset * or length combination is invalid, this method will throw an IllegalArgumentException. * @param length The length (from the offset) to conver the bytes. If the offset * or length combination is invalid, this method will throw an IllegalArgumentException. */ public static String toHexString(byte[] bytes, int offset, int length) { if (bytes == null) { return ""; } assertOffsetLengthValid(offset, length, bytes.length); // each byte is 2 chars in string StringBuilder buffer = new StringBuilder(length * 2); appendHexString(buffer, bytes, offset, length); return buffer.toString(); }
/** * Appends a byte array to a StringBuilder with each byte in a "Big Endian" * hexidecimal format. For example, a byte 0x34 will be appended as a * String in format "34". A byte array of { 0x34, 035 } would append "3435". * @param buffer The StringBuilder the byte array in hexidecimal format * will be appended to. If the buffer is null, this method will throw * a NullPointerException. * @param bytes The byte array that will be converted to a hexidecimal String. * If the byte array is null, this method will append nothing (a noop) * @param offset The offset in the byte array to start from. If the offset * or length combination is invalid, this method will throw an IllegalArgumentException. * @param length The length (from the offset) to conver the bytes. If the offset * or length combination is invalid, this method will throw an IllegalArgumentException. */ static public void appendHexString(StringBuilder buffer, byte[] bytes, int offset, int length) { assertNotNull(buffer); if (bytes == null) { return; // do nothing (a noop) } assertOffsetLengthValid(offset, length, bytes.length); int end = offset + length; for (int i = offset; i < end; i++) { int nibble1 = (bytes[i] & 0xF0) >>> 4; int nibble0 = (bytes[i] & 0x0F); buffer.append(HEX_TABLE[nibble1]); buffer.append(HEX_TABLE[nibble0]); } }
/** * Appends a byte array to a StringBuilder with each byte in a "Big Endian" * hexidecimal format. For example, a byte 0x34 will be appended as a * String in format "34". A byte array of { 0x34, 035 } would append "3435". * @param buffer The StringBuilder the byte array in hexidecimal format * will be appended to. If the buffer is null, this method will throw * a NullPointerException. * @param bytes The byte array that will be converted to a hexidecimal String. * If the byte array is null, this method will append nothing (a noop) * @param offset The offset in the byte array to start from. If the offset * or length combination is invalid, this method will throw an IllegalArgumentException. * @param length The length (from the offset) to conver the bytes. If the offset * or length combination is invalid, this method will throw an IllegalArgumentException. */ static public void appendHexString(StringBuilder buffer, byte[] bytes, int offset, int length) { assertNotNull(buffer); if (bytes == null) { return; // do nothing (a noop) } assertOffsetLengthValid(offset, length, bytes.length); int end = offset + length; for (int i = offset; i < end; i++) { int nibble1 = (bytes[i] & 0xF0) >>> 4; int nibble0 = (bytes[i] & 0x0F); buffer.append(HEX_TABLE[nibble1]); buffer.append(HEX_TABLE[nibble0]); } }
return null; assertOffsetLengthValid(offset, length, hexString.length());
return null; assertOffsetLengthValid(offset, length, hexString.length());