Convenience method to convert a byte array to a hex string.
class Main{
/**
* Convenience method to convert a byte array to a hex string.
*
* @param data
* the byte[] to convert
* @return String the converted byte[]
*/
public static String bytesToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
buf.append(byteToHex(data[i]));
}
return buf.toString();
}
/**
* Convenience method to convert a byte to a hex string.
*
* @param data
* the byte to convert
* @return String the converted byte
*/
public static String byteToHex(byte data) {
StringBuffer buf = new StringBuffer();
buf.append(toHexChar((data >>> 4) & 0x0F));
buf.append(toHexChar(data & 0x0F));
return buf.toString();
}
/**
* Convenience method to convert an int to a hex char.
*
* @param i
* the int to convert
* @return char the converted char
*/
public static char toHexChar(int i) {
if ((0 <= i) && (i <= 9))
return (char) ('0' + i);
else
return (char) ('a' + (i - 10));
}
}
Related examples in the same category