016如何转换成二进制数

问题描述:

016如何转换成二进制数

/*
00开始为二进制、0x或0X开始的为十六进制、0开始为八进制、正常的为十进制其中0为数字零
*/
public class To {
/**
* 将给定的一个进制数 转换成其它三种进制数
* 如:给定一个十进制数 转换成二进制数、八进制数和十六进制数
* 而:给定一个二进制数 转换成八进制数、十进制数和十六进制数
*/
private static String error = null;
private static char[] a = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
/**
* 给定一个数 将其转换
*/
public static void main(String[] args) {
String number = "016";
int to10 = to10(number);
System.out.println(number + "二进制: " + toNumber(to10, 2));
System.out.println(number + "八进制: " + toNumber(to10, 8));
System.out.println(number + "十进制: " + toNumber(to10, 10));
System.out.println(number + "十六进制: " + toNumber(to10, 16));
}
public static String toNumber(int number, int n) {
String str = "";
for (int i = 0; i if (number == i) {
str = a[i] + str;
return str;
}
}
str = a[number % n] + str;
str = toNumber(number / n, n) + str;
return str;
}
/**
* 将给定的数 转换成十进制数
*/
public static int to10(String str) {
int number = 0;
int status = 10;
if (str.charAt(0) == '0') {
if (str.charAt(1) == 'x' || str.charAt(1) == 'X') {
status = 16;
str = str.substring(2);
} else if (str.charAt(1) == '0') {
status = 2;
str = str.substring(2);
} else {
status = 8;
str = str.substring(1);
}
}
number = stringToInt(str, status);
return number;
}
public static int stringToInt(String str, int status) {
int number = 0;
int k = 0;
int s = 1;
int leng = str.length() - 1;
for (int i = str.length() - 1; i >= 0; i--) {
if (error != null) {
break;
}
if(k > 0) {
s *= status;
}
System.out.println(s);
switch (str.charAt(i)) {
case '0':
number = number + 0 * s;
k++;
break;
case '1':
number = number + 1 * s;
k++;
break;
case '2':
if (2 >= status) {
error = "数据出错!";
break;
}
number = number + 2 * s;
k++;
break;
case '3':
if (3 >= status) {
error = "数据出错!";
break;
}
number = number + 3 * s;
k++;
break;
case '4':
if(4 >= status) {
error = "数据出错!";
break;
}
number = number + 4 * s;
k++;
break;
case '5':
if(5 >= status) {
error = "数据出错!";
break;
}
number = number + 5 * s;
k++;
break;
case '6':
if(6 >= status) {
error = "数据出错!";
break;
}
number = number + 6 * s;
k++;
break;
case '7':
if(7 >= status) {
error = "数据出错!";
break;
}
number = number + 7 * s;
k++;
break;
case '8':
if(8 >= status) {
error = "数据出错!";
break;
}
number = number + 8 * s;
k++;
break;
case '9':
if(9 >= status) {
error = "数据出错!";
break;
}
number = number + 9 * s;
k++;
break;
case 'A':
if(10 >= status) {
error = "数据出错!";
break;
}
number = number + 10 * s;
k++;
break;
case 'B':
if(11 >= status) {
error = "数据出错!";
break;
}
number = number + 11 * s;
k++;
break;
case 'C':
if(12 >= status) {
error = "数据出错!";
break;
}
number = number + 12 * s;
k++;
break;
case 'D':
if(13 >= status) {
error = "数据出错!";
break;
}
number = number + 13 * s;
k++;
break;
case 'E':
if(14 >= status) {
error = "数据出错!";
break;
}
number = number + 14 * s;
k++;
break;
case 'F':
if(15 >= status) {
error = "数据出错!";
break;
}
number = number + 15 * s;
k++;
break;
default:
error = "数据出错!";
break;
}
}
if(error != null) {
System.out.println(error);
}
return number;
}
}