public class 二进制转十进制 {
public static void main(String[] args) {
System.out.println(bin2Oct("110011"));
}
private static double bin2Oct(String str) {
char[] cs = str.toCharArray();
double sum = 0;
for (int i = 0; i < cs.length; i++) {
sum = sum + (cs[i] - 48) * Math.pow(2, cs.length - i - 1);
}
return sum;
}
}
public class 十进制转二进制 {
public static void main(String[] args) {
System.out.println(convert(8,2));
}
static String convert(int source, int jinzhi) {
int beichushu = source;
StringBuffer sb = new StringBuffer(8);
int shang = 0;
int chushu = jinzhi;
int yushu = 0;
do {
shang = beichushu / chushu;
yushu = beichushu % chushu;
sb.insert(0, yushu);
if (shang == 0) {
break;
}
beichushu = shang;
} while (true);
return sb.toString();
}
}
原文:https://www.cnblogs.com/alichengxuyuan/p/12578010.html