2014-09-30 37 views
0

我正在尝试编写一个将二进制(有或没有小数部分)输入转换为十六进制的程序,该程序几乎完成,但不幸在十六进制输出中该点(".")失踪。在java -unexpected输出中将二进制/十进制小数转换为十六进制

假设我的预期产出是e7.6,但我得到的是e76

只有"."丢失。

这里是我的BinToHex类..

import java.io.*; 

//tried to convert the binary into dec and then dec to hex 
public class BinToHex { 
    double tempDec,fractionpart; 
    long longofintpart,templongDec; 
    String inpu ="11100111.011"; 
    String hexOutput=null,tempDecString,hex = null; 

    static int i = 1; 

    public void convertbintohex() { 

      if (inpu.contains(".")) { 

       int placesAfterPoint = inpu.length() - inpu.indexOf(".") - 1;//every thing 
       long numerator = Long.parseLong(inpu.replace(".", ""), 2);//goes 
       double decimalOfInput = ((double) numerator)/(1L << placesAfterPoint);//alright till here 


       while (true) { 
        tempDec = decimalOfInput * 16; 
        if (tempDec == (int)tempDec) { 
         tempDecString = String.valueOf((long)tempDec); 
         templongDec = Long.parseLong(tempDecString, 10); 
         hexOutput = Long.toHexString(templongDec); 

         break; 
        } else { 
         longofintpart = (long)tempDec; 
         hex=Long.toHexString(longofintpart); 
         if(i==1){ 
          hexOutput = hex + "."; 
          i=i+1; 
         }else{ 
          hexOutput = hexOutput + hex; 
         } 
         fractionpart = tempDec-(int)tempDec; 
         decimalOfInput = fractionpart; 
        } 
       } 
      } else { 
        // this part is ok 
       tempDecString = String.valueOf(Integer.parseInt(inpu, 2)); 
       templongDec = Long.parseLong(tempDecString, 10); 
       hexOutput = Long.toHexString(templongDec); 
      } 
      System.out.println(hexOutput); 
    } 
}  

我的主要测试类..

public class Test{ 
    public static void main(String args[]){ 
     BinToHex i = new BinToHex(); 
     i.convertbintohex();  
    } 
} 

我坚持! PLZ帮助。

+0

以及算法 - 我必须承认......我puzzeled您的代码完全^^ ^^你最终可以将这些代码分成更多的方法来指出你的工作吗?或者你可以提供一些注释来解释你在这个或那个代码块中做什么?我真的不想冒犯你,但即使在阅读了两遍和三遍之后,我也没有得到它^^另一点是,这个问题有相当一部分访客,但没有人敢回答,也许是因为上述原因 - 没有冒犯,真的! – 2014-09-30 06:29:11

+0

问题是'decimalOfInput'初始值设定项给它一个整数除以8的值。乘以16得到一个整数,所以没有放入一个周期的测试成功。我无法告诉你该如何解决它,因为像@MartinFrank一样,我不知道你在做什么。 – 2014-09-30 13:03:29

+0

好的,我告诉你出了什么问题^^ if(tempDec ==(int)tempDec)'会被达到!当它到达时,你调用'break;'当发生这种情况时,你永远不会到达代码'hexOutput = hex +“。”;'...所以我想你只是删除'break'语句...但这只是一个幸运的猜测......如果我是你,我会尝试分割字符串并使用'Integer.parseInt(str,16);'(十六进制输入)或'Integer.parseInt(str,2);'(二进制输入)为点之前和之后的字符串... – 2014-09-30 13:13:05

回答

0

最后找到了适当的算法转换为十进制(有或没有分数)到十六进制。

之外,binary(with or without fraction) to decimal in Java is here

转换十进制(有或没有分数)为十六进制在Java中

import java.math.*; 

public class DecimalToHex{ 
    public String decimalToHex(String decInpString){ 

     StringBuilder hexOut = new StringBuilder(); 
     double doubleOfDecInp = Double.parseDouble(decInpString); 

     if(doubleOfDecInp < 0){ 

      hexOut = hexOut.append("-"); 
      doubleOfDecInp = -doubleOfDecInp; 
     } 

     BigInteger beforedot = new BigDecimal(doubleOfDecInp).toBigInteger(); 
     hexOut.append(beforedot.toString(16)); 

     BigDecimal bfd =new BigDecimal(beforedot); 
     doubleOfDecInp = doubleOfDecInp - bfd.doubleValue(); 

     if(doubleOfDecInp == 0){ 
      return hexOut.toString(); 
     } 
     hexOut.append("."); 

     for (int i = 0; i < 16; ++i) { 
      doubleOfDecInp = doubleOfDecInp * 16; 
      int digit = (int)doubleOfDecInp; 

      hexOut.append(Integer.toHexString(digit)); 

      doubleOfDecInp = doubleOfDecInp - digit; 

      if (doubleOfDecInp == 0) 
       break; 
     } 

     return hexOut.toString(); 
    } 

    public static void main(String args[]){ 

     String decimalInp = "-0.767"; 
     String out ; 
     DecimalToHex i = new DecimalToHex(); 
     out = i.decimalToHex(decimalInp); 
     System.out.println(out);  

    } 
} 
1

真的,不是我无法抗拒写一个解决方案...有这样长的意见后,JST需要我几分钟^^

final int CODEBASE = 16; 
String input = "11100111.011"; 

//lets see if we have a '.' in our String 
if (input.indexOf(".") > 0) { 

    //yes, we have one - so we can split the string by '.' 
    String splits = input.split("."); 

    //the part left of the dot 
    String beforeDot = splits[0]; 

    //the part right of the dot 
    String afterDot = splits[1]; 

    //it's a incomplete input, we must fill up with 
    //trailing zeros according to out code base 
    afterDot.fillTrailingZeros(afterDot, CODEBASE); 

    //now we can parse the input 
    int asIntBefore = Integer.parseInt(beforeDots, 2); 
    int asIntAfter = Integer.parseInt(afterDot , 2); 

} else { 
    //use your working code for 
    //input wthoput dot HERE 
} 

//fills trailing zeros to input String 
String fillTrailingZeros(String input, int base){ 

    //as long as our String is shorter than the codebase... 
    while (input.length() < base){ 

     //...we have to add trailing zeros 
     input = input +"0"; 
    } 
    return input; 
} 
+0

看到评论如何工作? – 2014-09-30 13:24:46

+0

Thnx,但我不认为这将工作,如果二进制输入的小数部分超过四位数。并为评论,可能你没有正确阅读我的代码,也有我的代码中的评论,你错过了它莫名其妙。@ MartinFrank – Tiash 2014-09-30 16:50:08

+0

是啊...我想你是对的...对不起,我误解了你..对不起,因为麻烦... – 2014-09-30 19:03:22

相关问题