This part of the code is extracted from the web and is slightly collated for conversion between byte and hexadecimal.
/** * Reference Apache Commons <a * href= "http://commons.apache.org/codec/" >http://commons.apache.org/codec/ </a> * * Byte occupies 8 digits and hexadecimal characters occupy 4 bits. So you can convert a byte to two hexadecimal characters, which converts the high 4 bits and the lower 4 bits of byte to the corresponding hexadecimal characters H and l, and combine them.
The opposite is also true of the conversion. */public class Hex {/** * used to create hexadecimal character output * * private static final char[] Digits_lower = {' 0 ', ' 1 ', ' 2 ', '
3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; /** * Used to create the output of hexadecimal characters/private static final char[] Digits_upper = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', '
9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
/** * Converts a byte array to a hexadecimal character array.
* * Because two characters are used to represent a byte, the returned char[] length will be twice times the length of the parameter byte[]. * * @param data * for converting to hexadecimal characters byte[] * @return char[with 16 characters]/public static char[] Encodehex (final
Byte[] Data {return Encodehex (data, true);
/** * Converts a byte array to a hexadecimal character array.
* * Because two characters are used to represent a byte, the returned char[] length will be twice times the length of the parameter byte[]. * * @param data * byte[for converting to hexadecimal characters]
* @param tolowercase * <code>true</code> to lowercase format, <code>false</code> convert to uppercase format * @re Turn contains 16-character char[]/public static char[] Encodehex (final byte[] data, final Boolean tolowercase) {return enc Odehex (data, tolowercase?)
Digits_lower:digits_upper);
/** * Converts a byte array to a hexadecimal character array.
* * Because two characters are used to represent a byte, the returned char[] length will be twice times the length of the parameter byte[].
* * @param data * for converting to hexadecimal characters byte[] * @param todigits * The alphabet for controlling output * @return contains a 16-character char[]
* * 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;
/** * Converts a byte array to a hexadecimal string.
* * Because two characters are used to represent a byte, the string length returned is twice times the length of the parameter byte[].
* * @param data* byte[] * for converting to hexadecimal characters * @return hexadecimal string/public static string Encodehexstr (final byte[] data) {return E
NCODEHEXSTR (data, true);
/** * Converts a byte array to a hexadecimal string.
* * Because two characters are used to represent a byte, the string length returned is twice times the length of the parameter byte[]. * * @param data * for converting to hexadecimal characters byte[] * @param tolowercase * <code>true</code> converted to lowercase format, <code>false</code> to uppercase format * @return hexadecimal String */public static string Encodehexstr (byte[] data, Boolea N tolowercase) {return encodehexstr (data, tolowercase?)
Digits_lower:digits_upper);
/** * Converts a byte array to a hexadecimal string.
* * Because two characters are used to represent a byte, the string length returned is twice times the length of the parameter byte[]. * * @param data * for converting to hexadecimal characters byte[] * @param todigits * The alphabet * @return hexadecimal string for controlling output * * PR
otected static String encodehexstr (byte[] data, char[] todigits) {return new String (Encodehex (data, todigits)); /** * Converts a hexadecimal character array to an array of bytes * * @param data * hexadecimal char[] * @return byte[]
* @throws RuntimeException * If the length of the source hexadecimal character array is odd, it throws the Run-time exception/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, then the byte[] size is set to half 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;
/** * Converts a hexadecimal character to an integer. * @param ch * The character to be converted to an integer * @param the position of the index * character in the character array * @return an integer * @throws runtimeexcept Ion * When CH is not a valid hexadecimal character, throw the exception/protected static int todigit (final char ch, final int index) {final in
t 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 as 16:" + encodestr);
SYSTEM.OUT.PRINTLN ("Hexadecimal decoding is a string:" + decodestr);
}
}