public class Trans {
public static void main(String [] args){
toHex(26);
toBinary(-6);
toOctal(26);
//一下三句为
System.out.println(Integer.toHexString(26));
System.out.println(Integer.toBinaryString(-6));
System.out.println(Integer.toOctalString(26));
}
public static void toHex(int num){
//十进制--》十六进制
trans(num,15,4);
}
public static void toBinary(int num){
//十进制--> 二进制
trans(num,1,1);
}
public static void toOctal(int num){
//十进制-->八进制
trans(num,7,3);
}
public static void trans(int num,int base,int offset){
if(num==0)
{
System.out.println("0");
return;
}
//定义一个对应关系表
char[] chs={'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F'};
char[] arr=new char[32];
int pos = arr.length;
while(num!=0)
{
int temp = num & base;
arr[--pos]=chs[temp];
num=num >>> offset;
}
for(int x=pos;x<arr.length;x++)
{
System.out.print(arr[x]);
}
System.out.println();
}
}
--------------------Configuration: <Default>--------------------
1A
11111111111111111111111111111010
32
1a
11111111111111111111111111111010
32
Process completed.