早教吧作业答案频道 -->其他-->
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 < n; 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;
}
}
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 < n; 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;
}
}
看了 016如何转换成二进制数...的网友还看了以下:
数学/计算机:为什么这样翻转后可以将二进制数给翻转了呢01101100//0、1交换,1、0交换, 2020-04-27 …
问一行列式题行列式题 |x 1 1 1| |x+3 1 1 1||1-x x-1 1 1|=| 0 2020-05-14 …
①y=x,x=1,y=0②y=根号x,x=4,y=0绕x轴旋转产生的旋转体的体积;③y=x^2,y 2020-06-25 …
vb如何让数字只要有小数位就进一变为整数如0.1转换为1;0.8也是转换为1 2020-07-10 …
这个分式不等式如何转化整式不等式y/(1-y)≥0如何转化为y(y-1)≤0?老师说分式两边同时乘 2020-07-20 …
0.1*0.3*0.9+0.2*0.6*1.8+0.3*0.9*2.7/0.1*0.2*0.4+0 2020-07-26 …
数字图像处理图像绕点旋转几何变换怎么求1.设灰度级为8的子图像f(x,y)=[01267;12345 2020-10-31 …
关于次方我刚才知道了2进制转10进制怎么转1111=15F;1*2^0+1*2^1+1*2^+1*2 2020-11-15 …
(改编)圆锥旋转如图,点A,B,C在直角坐标系中的坐标为A(1,0),B(3,0),C(0,1),则 2020-12-25 …
为什么0乘以x(x为任何数)=0是成立的为什么0乘以x(x为任何数)=0是成立的?这个式子不是可以转 2021-02-03 …
相关搜索:016如何转换成二进制数