Code with Finding: |
class Hex {
/**
* Converts the byte <code>b</code> to capitalized hexadecimal text.
* The result will have length 2 and only contain the characters '0', '1',
* '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'.
*
* @param b the byte to convert.
*
* @return capitalized hexadecimal text representation of <code>b</code>.
*/
public static String byteToHexString(byte b) {
int n = b & 0x000000FF;
String result = (n < 0x00000010 ? "0" : "") + Integer.toHexString(n);
return result.toUpperCase();
}
}
class Hex {
/**
* Hexadecimal representation of <code>data</code> with spaces between
* individual bytes.
*
* @param data the byte array to print.
*
* @return spaced hexadecimal representation of <code>data</code>.
*/
public static String bytesToSpacedHexString(byte[] data) {
String result = "";
for (int i = 0; i < data.length; i++) {
result += byteToHexString(data[i]);
result += (i < data.length - 1) ? " " : "";
}
result = result.toUpperCase();
return result;
}
}
|