1 public class Day042
2 {
3 public static void main(String[] args)
4 {
5 changeI(60);
6 System.out.print(toUnsignedString(60, 4));
7 }
8 static void changeI(int x)
9 {
10 int temp = x;
11 StringBuffer sb = new StringBuffer();
12 while ((temp & 15) >= 0)
13 {
14 int t = temp & 15;
15 char ch = (t > 9 ? (char) (t - 10 + 'a') : (char) (t + '0'));
16 sb = sb.insert(0, ch);
17 /*
18 * >> 右移,高位补符号位。temp >> 1,右移一位表示除2。 >>> 无符号右移,高位补0。temp >>
19 * 1,右移一位表示除2。 << 左移,左移一位表示乘2,二位就表示4,就是2的n次方。
20 */
21 if (temp >>> 4 == 0)
22 {
23 break;
24 } else
25 {
26 temp = temp >>> 4;
27 }
28 }
29 if (Integer.toHexString(x).equals(sb.toString()))
30 {
31 System.out.println(sb.toString() + " ");
32
33 } else
34 {
35 // 测试结果是否正确方法
36 System.out.println(Integer.toHexString(x).equals(sb.toString()));
37 System.out.println(Integer.toHexString(x));
38 System.out.println(sb);
39 }
40 }
41 // 此方法来源于 integer.class-->Integer.toHexString(i)-->toUnsignedString(int i,
42 // int shift);
43 // 参数i为要转换成16进制的10进制的值,shift等于4就是将10进制转化成16进制。
44 private static String toUnsignedString(int i, int shift)
45 {
46 char[] buf = new char[32];
47 int charPos = 32;
48 int radix = 1 << shift;
49 int mask = radix - 1;
50 do
51 {
52 buf[--charPos] = digits[i & mask];
53 i >>>= shift;
54 } while (i != 0);
55
56 return new String(buf, charPos, (32 - charPos));
57 }
58 final static char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
59 '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
60 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
61 'z'};
62 }