package base;
public class zhuanhuan {
public static void main(String[] args) {
toBin(60);
toOctal(60);
toHex(60);
toBin(-60);
toOctal(-60);
toHex(-60);
}
/**
* 十进制转为八进制
*/
public static void toOctal(int num) {
(num + "对应的八进制为:");
tran(num, 7, 3);
}
/**
* 十进制转为十六进制
*/
public static void toHex(int num) {
(num + "对应的十六进制为:");
tran(num, 15, 4);
}
/**
* 十进制转为二进制
*/
public static void toBin(int num) {
(num + "对应的二进制为:");
tran(num, 1, 1);
}
/**
* 十进制转为二、八、十六进制,其中num为要转换的十进制数,base为基位数,offset为位移位数
*/
public static void tran(int num, int base, int offset) {
char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char out[] = new char[32];
int pos = ;
while (num != 0) {
int temp = num & base;
out[--pos] = table[temp];
num = num >>> offset;
}
for (int i = pos; i < ; i++) {
(out[i]);
}
.println("\n--------------------------------------------
java十进制转换为二、八、十六进制 来自淘豆网m.daumloan.com转载请标明出处.