Koen Handekyn
Friday, June 20, 2003
How to turn a byte into it's hexadecimal representation in Java:
public static String toHex(byte a) {
final char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char first = digits[(a & 0xff) >> 4];
char second = digits[a & 0x0f];
StringBuffer hex = new StringBuffer(2);
hex.append(first);
hex.append(second);
return hex.toString();
}
However, it seems that similar functionality is availlable for Integers as member functions of the java.lang.Integer class.
The core need was a function that converts a byte[] into a String containing the hex representation of the array (Conversion.java).