Code with Finding: |
class Hex {
/**
* Converts the integer <code>n</code> to capitalized hexadecimal text.
* The result will have length 8 and only contain the characters '0', '1',
* '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'.
*
* @param n the integer to convert.
*
* @return capitalized hexadecimal text representation of <code>n</code>.
*/
public static String intToHexString(int n) {
String result = ((n < 0x10000000) ? "0" : "")
+ ((n < 0x01000000) ? "0" : "")
+ ((n < 0x00100000) ? "0" : "")
+ ((n < 0x00010000) ? "0" : "")
+ ((n < 0x00001000) ? "0" : "")
+ ((n < 0x00000100) ? "0" : "")
+ ((n < 0x00000010) ? "0" : "")
+ Integer.toHexString(n);
return result.toUpperCase();
}
}
class Hex {
/**
* Hexadecimal representations of <code>data</code> with spaces between
* individual bytes. Each string represents <code>columns</code> bytes,
* except for the last one, which represents at most <code>columns</code>
* bytes.
*
* @param data the byte array to represent.
* @param columns the width of each line.
* @param padWidth resulting strings will be padded to this length with
* spaces to the right.
*
* @return spaced hexadecimal representations of <code>data</code>.
*/
private static String[] bytesToSpacedHexStrings(byte[] data, int columns,
int padWidth) {
byte[][] src = split(data,columns);
String[] result = new String[src.length];
for (int j = 0; j < src.length; j++) {
result[j] = bytesToSpacedHexString(src[j]);
result[j] = pad(result[j],padWidth,' ',RIGHT);
}
return result;
}
}
|