Java converts bytes to hexadecimal code sharing
- 2020-04-01 04:35:34
- OfStack
Some of the code in this article is excerpted from the web and tidy-up for converting between bytes and hexadecimal.
/**
* reference apache commons <a
* href="http://commons.apache.org/codec/">http://commons.apache.org/codec/</a>
*
* byte Take up 8 Bit, hexadecimal character occupancy 4 position So we can take one byte Convert to two corresponding hexadecimal characters byte The high 4 And the low 4 position * Convert to corresponding hexadecimal characters respectively H and L, And put them together. The same is true for the opposite transformation.
*
*/
public class Hex {
private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
public static char[] encodeHex(final byte[] data) {
return encodeHex(data, true);
}
/**
* Converts a byte array to a hexadecimal character array.
*
* Returns because two characters represent a byte char[] Length will be the parameter byte[] Twice the length.
*
* @param data
* Used to convert to hexadecimal characters byte[]
* @param toLowerCase
* <code>true</code> Convert to lowercase . <code>false</code> Transfer to uppercase
* @return Containing hexadecimal characters char[]
*/
public static char[] encodeHex(final byte[] data, final boolean toLowerCase) {
return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
}
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;
}
public static String encodeHexStr(final byte[] data) {
return encodeHexStr(data, true);
}
/**
* Converts a byte array to a hexadecimal string.
*
* Because two characters represent a byte, the length of the returned string will be an argument byte[] Twice the length.
*
* @param data
* Used to convert to hexadecimal characters byte[]
* @param toLowerCase
* <code>true</code> Convert to lowercase . <code>false</code> Transfer to uppercase
* @return Hexadecimal string
*/
public static String encodeHexStr(byte[] data, boolean toLowerCase) {
return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
}
protected static String encodeHexStr(byte[] data, char[] toDigits) {
return new String(encodeHex(data, toDigits));
}
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 hexadecimal characters, and the byte[] size is set to half the size of the 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;
}
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(" String encoded in hexadecimal: " + encodeStr);
System.out.println(" Hexadecimal decoding as a string: " + decodeStr);
}
}