求java编程 二进制十进制互相转化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求java编程 二进制十进制互相转化相关的知识,希望对你有一定的参考价值。

求求求。

java中有现在的api

10进制转二进制

Integer.toBinaryString(int i);


二进制转十进制

Integer.parseInt("1001",2);



建议多看看JDK文档,这里文档里都有介绍的


参考技术A 下面是十进制转换成二进制和二进制转换成十进制 根据你自己的需求选择
//10进制->2进制
String a = "-1195";//输入数值
BigInteger src = new BigInteger(a);//转换为BigInteger类型
System.out.println(src.toString(2));//转换为2进制并输出结果

//2进制->10进制
String a = "10010101011";//输入数值
BigInteger src = new BigInteger(a, 2);//转换为BigInteger类型
System.out.println(src.toString());//转换为2进制并输出结果

注意下面两个要点:
1. BigInteger的构造函数
BigInteger(String src)默认参数字符串为10进制数值
BigInteger(String src, int x)第2个参数x是指定第一个参数src的进制类型

2. toString方法
toString()默认把数值按10进制数值转化为字符串。
toString(int x)把数值按参数x的进制转化为字符串。
参考技术B //二进制转十进制数字
public static int binaryToDec(String binaryValue)
int dec = 0;
for(int i=0; i<binaryValue.length(); i++)
dec = dec * 2 + (binaryValue.charAt(i) - '0');

return dec;


//十进制转二进制字符串
public static String decToBinary(int decValue)
StringBuilder binaryValue = new StringBuilder();
do
int temp = decValue % 2;
binaryValue.append((char)(temp + '0'));
decValue /= 2;
while(decValue != 0);
return binaryValue.reverse().toString();

参考技术C public static void main(String args[])
String s = "101011110001";
int x = 0;
for(char c: s.toCharArray())
x = x * 2 + (c == '1' ? 1 : 0);
System.out.println(x);

String a="010";
int d = Integer.parseInt(a, 2); // 2进制
int o = Integer.parseInt(a, 8); // 8进制
System.out.println(d);
System.out.println(o);

java-十进制八进制十六进制数互相转换

java-十进制、八进制、十六进制数互相转换
下面是一个十进制、八进制、十六进制数互相转换的小程序
public class JZTurn {
?
????public static void main(String[] args) {
????????int num2 = 1314521;
????????System.out.println("十进制数:"+num2);
????????System.out.println("十进制转换二进制:"+turn2(num2));
????????System.out.println("十进制转换八进制:"+turn8(num2));
????????System.out.println("十进制转换十六进制:"+turn16(num2));
?????????
????????//2-> 8
????????System.out.println("二进制转换八进制:"+twoTurnEight(turn2(num2)));
????????System.out.println("八进制转换二进制:"+eightTurnTwo(turn8(num2)));
????????System.out.println("八进制转换十六进制:"+eightTurnSixteen(turn8(num2)));
????????//2->16
????????System.out.println("二进制转换十六进制:"+twoTurnSixteen(turn2(num2)));
????}
?????
????/
????? 10进制转二进制
?????
@param num
????? @return
?????
/
????public static String turn2(int num){
????????String shortTimeNum = "";
????????int remainder = 0;
????????/

????????? 模拟短除法
?????????
/
????????while(num >= 1){
????????????remainder = num % 2;
????????????num = num / 2;
????????????shortTimeNum = remainder + shortTimeNum;
????????}
?????????
????????return shortTimeNum;
????}
?????
????/
????? 10进制转8进制
?????
@param num
????? @return
?????
/
????public static String turn8(int num){
????????String shortTimeNum = "";
????????int remainder = 0;
????????/

????????? 模拟短除法
?????????
/
????????while(num >= 1){
????????????remainder = num % 8;
????????????num = num / 8;
????????????shortTimeNum = remainder + shortTimeNum;
????????}
?????????
????????return shortTimeNum;
????}
?????
????/
????? 10进制转16进制
?????
@param num
????? @return
?????
/
????public static String turn16(int num){
????????String shortTimeNum = "";
????????int remainder = 0;
????????String tempStr = "";
????????/

????????? 模拟短除法
?????????
/
????????while(num >= 1){
????????????remainder = num % 16;
????????????tempStr = getOtherNum(remainder);
?????????????
????????????num = num / 16;
????????????shortTimeNum = tempStr + shortTimeNum;
????????}
?????????
????????return shortTimeNum;
????}
?????
????/
????? 2进制转换8进制
?????
概念说明:这里转换的是整数,从右向左三位一组分别乘以2的零次方,2的一次方,2的2次方
????? 然后把每组中的数相加,再把各组从左向右拼接到一起
?????
@param strNum2
?????/
????public static String twoTurnEight(String strNum2){
????????String resultNum = "";
????????int remainder = strNum2.length() % 3;
?????????
????????//补位 三位一组,最后一组位数不够用0补充
????????switch(remainder){
????????case 1:
????????????strNum2 = "00"+strNum2;
????????????break;
????????case 2:
????????????strNum2 = "0"+strNum2;
????????????break;
????????}
????????int index = strNum2.length()-1;
????????int tempNum1 = 0;
????????int tempNum2 = 0;
????????int tempNum3 = 0;
????????while(index>=1){
?
????????????index -- ;
?????????????
????????????if(index % 3 == 0){
????????????????//每个数为一组
????????????????tempNum1 = Integer.parseInt(strNum2.substring(index, index+1));
????????????????tempNum2 = Integer.parseInt(strNum2.substring(index+1, index+2));
????????????????tempNum3 = Integer.parseInt(strNum2.substring(index+2, index+3));
?????????????????
????????????????resultNum = tempNum3
1+tempNum22+tempNum14+resultNum;
?????????????????
????????????}
?????????????
????????}
?????????
????????return resultNum;
????}
?????
????/

????? 八进制转换二进制
?????
@param eightNum
????? @return
?????
/
????public static String eightTurnTwo(String eightNum){
????????String twoNum = "";
????????int index = eightNum.length()-1;
?????????
????????String tempNum = "";
????????while(index>=0){
?????????????
????????????tempNum = turn2(Integer.parseInt(eightNum.substring(index, index+1))) ;
?????????????
????????????//补位,在转换8进制时是每三为二进制数为一组,转换回二进制时位数也需要是三位,不够用零补
????????????switch (tempNum.length()) {
?????????????
????????????case 1:
????????????????tempNum = "00"+tempNum;
????????????????break;
?
????????????case 2:
????????????????tempNum = "0"+tempNum;
????????????????break;
????????????}
?????????????
????????????twoNum = tempNum + twoNum;
????????????index--;
????????}
?????????
????????return twoNum;
????}
?????
????/
????? 2进制转换16进制
?????
概念说明:这里转换的是整数,从右向左四位一组分别乘以2的零次方,2的一次方,2的2次方,2的3次方,
????? 然后相加把每组最终的得数一次从左向右拼到一起,若其中一组的和大于9,按照对应关系转换后再把每组的结果拼接到一起
?????
@param strNum2
?????/
????public static String twoTurnSixteen(String strNum2){
????????String resultNum = "";
????????int remainder = strNum2.length() % 4;
?????????
????????//补位,四位一组,最后一组位数不够用0补充
????????switch(remainder){
????????case 1:
????????????strNum2 = "000"+strNum2;
????????????break;
????????case 2:
????????????strNum2 = "00"+strNum2;
????????????break;
????????case 3:
????????????strNum2 = "0"+strNum2;
????????????break;
????????}
????????int index = strNum2.length()-1;
????????int tempNum1 = 0;
????????int tempNum2 = 0;
????????int tempNum3 = 0;
????????int tempNum4 = 0;
????????int tempNum5 = 0;
????????while(index>=1){
?
????????????index -- ;
?????????????
????????????if(index % 4 == 0){
????????????????//每个数为一组
????????????????tempNum1 = Integer.parseInt(strNum2.substring(index, index+1));
????????????????tempNum2 = Integer.parseInt(strNum2.substring(index+1, index+2));
????????????????tempNum3 = Integer.parseInt(strNum2.substring(index+2, index+3));
????????????????tempNum4 = Integer.parseInt(strNum2.substring(index+3, index+4));
?????????????????
????????????????tempNum5 = tempNum4
1 + tempNum32+tempNum24+tempNum1*8;
?????????????????
????????????????resultNum = getOtherNum(tempNum5) +resultNum;
?????????????????
????????????}
?????????????
????????}
?????????
????????return resultNum;
????}
?????
????/*
?????
8进制转换16进制
????? @param strNum2
?????
/
????public static String eightTurnSixteen(String strNum2){
????????return twoTurnSixteen(eightTurnTwo(strNum2));
????}
?????
????/

????? 16进制转换中的特殊处理,需要把大于9的数字转换成字母
?????
@param tempNum
????? @return
?????
/
????public static String getOtherNum(int tempNum){
????????String tempStr = "";
????????if(tempNum > 9){
????????????switch(tempNum){
????????????case 10:
????????????????tempStr = "A";
????????????????break ;
????????????case 11:
????????????????tempStr = "B";
????????????????break;
????????????case 12:
????????????????tempStr = "C";
????????????????break;
????????????case 13:
????????????????tempStr = "D";
????????????????break;
????????????case 14:
????????????????tempStr = "E";
????????????????break;
????????????case 15:
????????????????tempStr = "F";
????????????????break;
????????????}
????????}else{
????????????tempStr = String.valueOf(tempNum);
????????}
????????return tempStr;
????}
}
输出结果:
十进制数:1314521
十进制转换二进制:101000000111011011001
十进制转换八进制:5007331
十进制转换十六进制:140ED9
二进制转换八进制:5007331
八进制转换二进制:101111011011001
八进制转换十六进制:5ED9
二进制转换十六进制:140ED9
喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!需要更多java学习资料的也可以私信我!
祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!

以上是关于求java编程 二进制十进制互相转化的主要内容,如果未能解决你的问题,请参考以下文章

新手求问lua能将十进制和十六进制数互相转化吗

16进制与10进制怎么互相转换

剑指offer知识点Java中无符号整型如何与int/long类型互相转化

剑指offer知识点Java中无符号整型如何与int/long类型互相转化

剑指offer知识点Java中无符号整型如何与int/long类型互相转化

编码二以及is,小数据池