在java -unexpected输出中将二进制/小数部分转换为十六进制

时间:2014-09-30 05:42:55

标签: java debugging binary hex

我正在尝试编写一个程序,将二进制(有或没有分数)输入转换为十六进制,这几乎已经完成但不幸的是在十六进制输出中缺少点(".")。

假设我的预期输出为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();    
    }
}

我被卡住了! 请帮助。

2 个答案:

答案 0 :(得分:1)

真的,不是我无法抗拒写一个解决方案......经过这么长时间的评论后,它花了我一些时间^^

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;
}

答案 1 :(得分: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);    

    }
}