/*
需求:写一个关于进制转换的类
*/
class ArrayTest {
private static void trans(int num, int yu, int yi) {
//定义一个进制表
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 & yu;
arr[--pos] = chs[temp];
//num右移
num = num >>> yi;
}
for(int i = pos; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
}
public static void main(String[] args) {
//二进制
toBin(6);
System.out.println();
//十进制
toBa(60);
System.out.println();
//十六进制
toHex(60);
}
//转换成二进制
public static void toBin(int num) {
trans(num, 1, 1);
}
//转换成八进制
public static void toBa(int num) {
trans(num, 7, 3);
}
//转换成十六进制
public static void toHex(int num) {
trans(num, 15, 4);
}
} |
|