1 public static void toBinary(int num){ 2 trans(num,1,1); 3 } 4 public static void toHex(int num){ 5 trans(num,15,4); 6 } 7 public static void toOctal(int num){ 8 trans(num,7,3); 9 } 10 public static void trans(int num,int base,int offset){ 11 if(num==0){ 12 System.out.println('0'); 13 return; 14 } 15 char[] chs={'0','1','2','3', 16 '4','5','6','7', 17 '8','9','A','B', 18 'C','D','E','F'}; 19 char[] ch = new char[32]; 20 int pos=ch.length; 21 while(num!=0){ 22 23 int temp = num & base; 24 ch[--pos] = chs[temp]; 25 num = num >>> offset; 26 } 27 //System.out.println("pos="+pos); 28 for(int x=pos;x<ch.length;x++){ 29 System.out.print(ch[x]); 30 } 31 System.out.println(); 32 }