将二进制数转换为十进制数
发布网友
发布时间:2022-04-21 19:55
我来回答
共3个回答
热心网友
时间:2022-06-18 21:50
package com.test.numsys;
import java.util.*;
/**
* 数值转换 十进制 二进制 八进制 十六进制
*/
public class NumSys {
public static void main(String[] args) {
NumSys ns = new NumSys();
System.out.println("十进制100转换为二进制为:" + ns.decimalToBinary(100));//1100100
System.out.println("二进制1100100转换为八进制为: " + ns.binaryToOctal(1100100));// 144
System.out.println("二进制10110000011101转换为十六进制为:" + ns.binaryToHex("10110000011101")); //2C1D
System.out.println("十进制100转换为八进制:" + ns.DecimalToOctal(100));
System.out.println("十进制15400转换为十六进制:" + ns.DecimalToHex(15400));
}
//没有实现小数部分的转化 十进制转换为二进制
public long decimalToBinary(int a) {
long binary = 0L;
int[] binaryArr = new int[64];
int i = 0;
do {
binaryArr[i] = a%2;
a = a/2;
i ++;
} while(a != 0);
String s = "";
for(int j = binaryArr.length-1; j>=0; j--) {
s+=binaryArr[j] + "";
}
binary = Integer.parseInt(s);
return binary;
}
//二进制转换为八进制
public String binaryToOctal(long a) {
String octal = "";
String s = a +"";
char [] c_temp = (a+"").toCharArray();
int temp = 3 - c_temp.length%3;
for(int i = 0; i<temp ; i++) {
s = "0" + s;
}
char[] c2 = s.toCharArray();
List<String> list = new ArrayList<String>();
for(int i = 0 ; i < c2.length ; i += 3 ) {
String stemp = c2[i] +""+ c2[i+1] +""+ c2[i+2];
if(stemp.equals("000")) {
list.add("0");
} else if(stemp.equals("001")) {
list.add("1");
} else if(stemp.equals("010")) {
list.add("2");
} else if(stemp.equals("011")) {
list.add("3");
} else if(stemp.equals("100")) {
list.add("4");
} else if(stemp.equals("101")) {
list.add("5");
} else if(stemp.equals("110")) {
list.add("6");
} else if(stemp.equals("111")) {
list.add("7");
}
}
for(Iterator<String> it = list.iterator(); it.hasNext();) {
octal += it.next();
}
return octal;
}
public String binaryToHex(String a) {
String hex = "";
String s = a;
char [] c_temp = (a+"").toCharArray();
int temp = 4 - c_temp.length%4;
for(int i = 0; i<temp ; i++) {
s = "0" + s;
}
//System.out.println("-----"+s);
char[] c2 = s.toCharArray();
List<String> list = new ArrayList<String>();
for(int i = 0 ; i < c2.length ; i += 4 ) {
String stemp = c2[i] +""+ c2[i+1] +""+ c2[i+2] +""+ c2[i+3];
if(stemp.equals("0000")) {
list.add("0");
} else if(stemp.equals("0001")) {
list.add("1");
} else if(stemp.equals("0010")) {
list.add("2");
} else if(stemp.equals("0011")) {
list.add("3");
} else if(stemp.equals("0100")) {
list.add("4");
} else if(stemp.equals("0101")) {
list.add("5");
} else if(stemp.equals("0110")) {
list.add("6");
} else if(stemp.equals("0111")) {
list.add("7");
} else if(stemp.equals("1000")) {
list.add("8");
} else if(stemp.equals("1001")) {
list.add("9");
} else if(stemp.equals("1010")) {
list.add("A");
} else if(stemp.equals("1011")) {
list.add("B");
} else if(stemp.equals("1100")) {
list.add("C");
} else if(stemp.equals("1101")) {
list.add("D");
} else if(stemp.equals("1110")) {
list.add("E");
} else if(stemp.equals("1111")) {
list.add("F");
}
}
for(Iterator<String> it = list.iterator(); it.hasNext();) {
hex += it.next();
}
return hex;
}
//十进制转换为八进制
public String DecimalToOctal(int d)
{
String o = "";
if (d < 8)
{
o = d+"";
}
else
{
int c;
int s=0;
int n=d;
while (n >= 8)
{
s++;
n = n / 8;
}
int[] m = new int[s];
int i = 0;
do
{
c = d / 8;
m[i++] = d % 8;
d = c;
} while (c >= 8);
o = d+"";
for (int j = m.length - 1; j >= 0; j--)
{
o += m[j];
}
}
return o;
}
//十进制转十六进制
public String DecimalToHex(int d)
{
String x = "";
if (d < 16)
{
x = chang(d);
}
else
{
int c;
int s = 0;
int n = d;
while (n >= 16)
{
s++;
n = n / 16;
}
String [] m = new String[s];
int i = 0;
do
{
c = d / 16;
m[i++] = chang(d % 16);//判断是否大于10,如果大于10,则转换为A~F的格式
d = c;
} while (c >= 16);
x = chang(d);
for (int j = m.length - 1; j >= 0; j--)
{
x += m[j];
}
}
return x;
}
//判断是否为10~15之间的数,如果是则进行转换
public String chang(int d)
{
String x = "";
switch (d)
{
case 10:
x = "A";
break;
case 11:
x = "B";
break;
case 12:
x = "C";
break;
case 13:
x = "D";
break;
case 14:
x = "E";
break;
case 15:
x = "F";
break;
default:
x = d+"";
break;
}
return x;
}
}
热心网友
时间:2022-06-18 21:51
(1)(10010)二
=(((((0*2+1)*2+0)*2+0)*2+1)*2+0)十
=((((1*2+0)*2+0)*2+1)*2+0)十
=(((2*2+0)*2+1)*2+0)十
=((4*2+1)*2+0)十
=(9*2+0)十
=(18)十
(2)(1010.01)
(1010)二
=((((0*2+1)*2+0)*2+1)*2+0)十
=(((1*2+0)*2+1)*2+0)十
=((2*2+1)*2+0)十
=(5*2+0)十
=(10)十
(0.01)二
=((1/2+0)/2)十
=((0.5+0)/2)十
=(0.5/2)十
=(0.25)十
(1010.01)二
=(10.25)十
(3)(101001)二
=((((((0*2+1)*2+0)*2+1)*2+0)*2+0)*2+1)十
=(((((1*2+0)*2+1)*2+0)*2+0)*2+1)十
=((((2*2+1)*2+0)*2+0)*2+1)十
=(((5*2+0)*2+0)*2+1)十
=((10*2+0)*2+1)十
=(20*2+1)十
=(41)十
(4)(11001)二
=(((((0*2+1)*2+1)*2+0)*2+0)*2+1)十
=((((1*2+1)*2+0)*2+0)*2+1)十
=(((3*2+0)*2+0)*2+1)十
=((6*2+0)*2+1)十
=(12*2+1)十
=(25)十
热心网友
时间:2022-06-18 21:51
直接乘以10的1次方,二次方,这样一直下去救可以了