Code with Finding: |
class Hex {
/**
* Converts the short <code>s</code> to capitalized hexadecimal text.
* The result will have length 4 and only contain the characters '0', '1',
* '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'.
*
* @param s the short to convert.
*
* @return capitalized hexadecimal text representation of <code>s</code>.
*/
public static String shortToHexString(short s) {
int n = s & 0x0000FFFF;
String result = ((n < 0x00001000) ? "0" : "")
+ ((n < 0x00000100) ? "0" : "")
+ ((n < 0x00000010) ? "0" : "")
+ Integer.toHexString(s);
return result.toUpperCase();
}
}
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();
}
}
|