Well then have a look at the implementation of Integer.toHexString(int)
. The following code is extracted from the Integer
class in the java standard library.
public class Test {
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f'
};
private static String intAsHex(int i) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << 4;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= 4;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
public static void main(String... args) {
System.out.println(intAsHex(77));
}
}
Output: 4d