先说下整体思路:
包含小数的十六进制,我们可以先把它分为整数和小数两部分,
然后在进行字符串比较,进行相应的替换。
包含二进制的小数,思路差不多,需要注意的是左边整数不够四位的补0,右边也如此。
import java.math.BigDecimal;
public class Test13 {
public static void main(String[] args) {//十六-二
// TODO 自动生成的方法存根
String e = "0.77";
String num1 = "";
String num2 = "";
if(e.indexOf(".")==-1)//这里进行判断是否是小数
{
num1 = e;
num2 = "0";
}
else
{
String[] s1 = e.split("\\.");//把小数分成两部分
num1 = s1[0];
num2 = s1[1];
}
System.out.println("整数为:"+num1);
System.out.println("小数为:"+num2);
String a1= "";
String a3= "";
String a2 = "";//最后输出
int i = 0, i1 = 0;
int j1 = 0;
String[] ej={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
String[] bj={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
//处理整数
if(num1.equals("0"))
{
a1 = "0";
}
else{
j1 = num1.length();
i1 = 0;
for(i = 0; i<j1;i++)
{
for(int j = 0; j<ej.length;j++)
{
System.out.println("截取的值:"+num1.substring(i1, i1+1));
if(num1.substring(i1, i1+1).equals(bj[j]))
{
System.out.println("j的值"+j);
a1 =a1+ ej[j];
i1 = i1 +1;
break;
}
}
}
System.out.println("整数十六进制转二进制为:"+a1);
}
//小数部分
if(num2.equals("0"))
{
a2 = a1;
}
else
{
j1 = num2.length();//计算循环次数
System.out.println(j1);
i1 =0;
for(i = 0; i<j1;i++)
{
//ej2[i]=num1.substring(i, i+2);
for(int j = 0; j<ej.length;j++)
{
System.out.println("截取的值:"+num2.substring(i1, i1+1));
if(num2.substring(i1, i1+1).equals(bj[j]))
{
System.out.println("j的值"+j);
a3 =a3+ ej[j];
i1 = i1 +1;
break;
}
}
}
System.out.println("小数十六进制转二进制为:"+a3);
a2 = a1+"."+a3;
}
System.out.println("十六进制转二进制为:"+a2);
}
}
//上面就是大部分代码了,八进制转二进制也是同样的道理
接下来我们看看二进制转十六进制:
String s = "100001.01"
double s = Double.valueOf(e);
String num1 =String.valueOf((int)s);//整数
int zj1 = (int) s;
BigDecimal b1 = new BigDecimal(e);
BigDecimal b2 = new BigDecimal(zj1);
double zj2 = b1.subtract(b2).doubleValue();//小数部分
String num0 = String.valueOf(zj2);
//如果没有小数点,直接使用split就会报错。。
String[] s1 = num0.split("\\.");
String num2 = s1[1];//直接取小数
System.out.println("整数为:"+num1);
System.out.println("小数为:"+num2);
String a1= "";
String a3= "";
//String a2 = "";//最后输出
int i = 0, i1 = 0;
int j1 = 0;
String[] ej={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
String[] bj={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
if(num1.equals("0"))
{
a1 = "0";
}
else
{
//处理整数
if((num1.length()%4)!=0)
{
j1 = 4-num1.length()%4;
for(i = 0;i<j1;i++)
{
num1 = "0"+num1;
}
System.out.println("补零 "+num1);//计算左边需要补几个0
}
j1 = num1.length()/4;
System.out.println(j1);
for(i = 0; i<j1;i++)
{
//ej2[i]=num1.substring(i, i+2);
for(int j = 0; j<ej.length;j++)
{
System.out.println("截取的值:"+num1.substring(i1, i1+4));
if(num1.substring(i1, i1+4).equals(ej[j]))
{
System.out.println("j的值"+j);
a1 =a1+ bj[j];
i1 = i1 +4;
break;
}
}
}
System.out.println("整数二进制转十六进制为:"+a1);
}
if(num2.equals("0"))
{
a2 = a1;
}
else
{
//小数部分
if((num2.length()%4)!=0)
{
j1 = 4-num2.length()%4;//需要添加几个0
for(i = 0;i<j1;i++)
{
num2 = num2+"0";//这里和整数的不同
}
System.out.println("补零 "+num2);
}
j1 = num2.length()/4;//计算循环次数
System.out.println(j1);
i1 =0;
for(i = 0; i<j1;i++)
{
//ej2[i]=num1.substring(i, i+2);
for(int j = 0; j<ej.length;j++)
{
System.out.println("截取的值:"+num2.substring(i1, i1+4));
//System.out.println(num1.substring(i, i+3));
//System.out.println("ej[j]的值:"+ej[j]);
//System.out.println(num1.substring(i, i+3).equals(ej[j]));
if(num2.substring(i1, i1+4).equals(ej[j]))
{
System.out.println("j的值"+j);
a3 =a3+ bj[j];
i1 = i1 +4;
break;
}
}
}
System.out.println("小数二进制转十六进制为:"+a3);
a2 = a1+"."+a3;
}
System.out.println("二进制转十六进制为:"+a2);
}
haha,上面比较乱,我好久之前写的了,没时间整理,处理整数和小数哪里有优化的地方,或许是当时想复杂了,将就着看吧。新手java小白的心得,有不足的地方,可以指点一二。不要和我说你怎么不用函数。。非要搞这么复杂,哈哈。八二和二八转换也和上面差不多