1 package demo; 2 3 public class TestHex { 4 private static String array[]={"0","1","2","3","4", 5 "5","6","7","8","9", 6 "A","B","C","D","E"}; 7 public static void main(String args[]){ 8 testHex(128); 9 } 10 11 /** 12 * 将10进制转换成16进制 13 * 直接将10进制与15进行与运算(二进制中每四位为一个16进制位) 14 * 0110 15 * 1111 16 * &= 17 * 0110 18 */ 19 20 public static void testHex(int num){ 21 int temp; 22 StringBuffer strb = new StringBuffer(); 23 for(int i=0;i<8;i++){ 24 temp=num&15; 25 strb.append(array[temp]); 26 num=num>>>4; 27 28 } 29 System.out.println(strb.reverse()); 30 //输出结果为:00000080 31 } 32 33 }