Some of the code in this article is excerpted from the Internet and slightly organized to convert between bytes and hexadecimal.
/** * reference apache commons <a * href="http://commons.apache.org/codec/">http://commons.apache.org/codec/</a> * byte occupies 8 bits, and hexadecimal characters occupies 4 bits. Therefore, one byte can be converted into two corresponding hexadecimal characters, that is, the high 4 and low 4 bits of byte are converted into the corresponding hexadecimal characters H and L respectively, and combined. The same applies to the opposite transformation. * */public class Hex { /** * Output for establishing hex characters*/ private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * Output for establishing hex characters*/ private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /** * Convert a byte array to a hexadecimal character array. * * Because two characters are used to represent a byte, the returned char[] length will be twice the length of the parameter byte[]. * * @param data * byte[] used to convert to hex characters * @return char[] containing hex characters */ public static char[] encodeHex(final byte[] data) { return encodeHex(data, true); } /** * Convert a byte array to a hex character array. * * Because two characters are used to represent a byte, the returned char[] length will be twice the length of the parameter byte[]. * * @param data * byte[] for converting to hex characters * @param toLowerCase * <code>true</code> Transfer to lowercase format, <code>false</code> Transfer to uppercase format* @return char[] containing hex characters */ public static char[] encodeHex(final byte[] data, final boolean toLowerCase) { return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); } /** * Convert a byte array to a hex character array. * * Because two characters are used to represent a byte, the returned char[] length will be twice the length of the parameter byte[]. * * @param data * byte[] for converting to hex characters * @param toDigits * Alphabet for controlling output * @return char[] containing hex characters */ protected static char[] encodeHex(final byte[] data, final char[] toDigits) { int l = data.length; char[] out = new char[l << 1]; // two characters form the hex value. for (int i = 0, j = 0; i < l; i++) { out[j++] = toDigits[(0xF0 & data[i]) >>> 4]; out[j++] = toDigits[0x0F & data[i]]; } return out; } /** * Convert a byte array to a hexadecimal string. * * Because two characters are used to represent a byte, the returned string length will be twice the length of the parameter byte[]. * * @param data * byte[] for converting to hex characters * @return hex string */ public static String encodeHexStr(final byte[] data) { return encodeHexStr(data, true); } /** * Convert a byte array to a hex string. * * Because two characters are used to represent a byte, the returned string length will be twice the length of the parameter byte[]. * * @param data * byte[] for converting to hex characters * @param toLowerCase * <code>true</code> Transfer to lowercase format, <code>false</code> Transfer to uppercase format* @return HexString */ public static String encodeHexStr(byte[] data, boolean toLowerCase) { return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); } /** * Convert a byte array to a hex string. * * Because two characters are used to represent a byte, the returned string length will be twice the length of the parameter byte[]. * * @param data * byte[] for converting to hex characters * @param toDigits * Alphabet for controlling output * @return Hex String */ protected static String encodeHexStr(byte[] data, char[] toDigits) { return new String(encodeHex(data, toDigits)); } /** * Convert hex characters array to byte array* * @param data * Hex char[] * @return byte[] * @throws RuntimeException * If the length of the source hex character array is an odd number, a runtime exception will be thrown*/ public static byte[] decodeHex(char[] data) { int len = data.length; if ((len & 0x01) != 0) { throw new RuntimeException("Odd number of characters."); } // A byte corresponds to two hex characters, set the byte[] size to half of the size of char[] byte[] out = new byte[len >> 1]; // two characters form the hex value. for (int i = 0, j = 0; j < len; i++) { int f = toDigit(data[j], j) << 4; j++; f = f | toDigit(data[j], j); j++; out[i] = (byte) (f & 0xFF); } return out; } /** * Convert hexadecimal characters into an integer. * * @param ch * Characters to be converted into integers * @param index * The position of the character in the character array* @return An integer* @throws RuntimeException * When ch is not a legal hexadecimal character, throw this exception*/ protected static int toDigit(final char ch, final int index) { final int digit = Character.digit(ch, 16); if (digit == -1) { throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index); } return digit; } public static void main(String[] args) { String srcStr = "HelloWorld!"; String encodeStr = encodeHexStr(srcStr.getBytes(), false); String decodeStr = new String(decodeHex(encodeStr.toCharArray())); System.out.println("Source String: " + srcStr); System.out.println("Standard encoded as hex: " + encodeStr); System.out.println("Hexadecimal decoded as string: " + decodeStr); } }