在java中将二进制/十进制分数转换为十六进制-意外输出

在java中将二进制/十进制分数转换为十六进制-意外输出,java,debugging,binary,hex,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

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

假设我的预期输出是
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);
    }   
}       
我被卡住了!
请帮忙。

真的,不是我忍不住要写一个解决方案。。。有这么长的评论之后,我花了几分钟的时间^^

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;
}
final int CODEBASE=16;
字符串输入=“11100111.011”;
//让我们看看字符串中是否有“.”
if(input.indexOf(“.”)大于0){
//是的,我们有一个-所以我们可以用“.”分割字符串
字符串拆分=输入。拆分(“.”);
//圆点的左边部分
字符串beforeDot=splits[0];
//点的右边部分
字符串afterDot=splits[1];
//这是一个不完整的输入,我们必须用
//根据out代码基的尾随零
afterDot.filltrailingzero(afterDot,CODEBASE);
//现在我们可以解析输入了
int-asIntBefore=Integer.parseInt(beforeDots,2);
int-asIntAfter=Integer.parseInt(afterDot,2);
}否则{
//使用您的工作代码
//在此处输入wthoput点
}
//将尾随的零填充到输入字符串
字符串filltrailingzero(字符串输入,int-base){
//只要我们的字符串比代码库短。。。
while(input.length()
终于找到了将十进制(带分数或不带分数)转换为十六进制的正确算法

此外,

在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);    

    }
}
import java.math.*;
公共级DecimalToHex{
公共字符串decimalToHex(字符串decInpString){
StringBuilder hexOut=新StringBuilder();
double doubleOfDecInp=double.parseDouble(decinString);
if(doubleOfDecInp<0){
hexOut=hexOut.append(“-”);
doubleOfDecInp=-doubleOfDecInp;
}
BigInteger beforedot=新的BigDecimal(doubleOfDecInp).toBigInteger();
hexOut.append(在dot.toString(16)之前);
BigDecimal bfd=新的BigDecimal(在点之前);
doubleOfDecInp=doubleOfDecInp-bfd.doubleValue();
如果(doubleOfDecInp==0){
返回hexOut.toString();
}
hexOut。追加(“.”);
对于(int i=0;i<16;++i){
doubleOfDecInp=doubleOfDecInp*16;
整数位数=(整数)双精度整数;
hexOut.append(Integer.toHexString(digital));
doubleOfDecInp=doubleOfDecInp-数字;
如果(doubleOfDecInp==0)
打破
}
返回hexOut.toString();
}
公共静态void main(字符串参数[]){
字符串小数点=“-0.767”;
串出;
DecimalToHex i=新的DecimalToHex();
out=i.decimalToHex(小数点);
System.out.println(out);
}
}

嗯,我必须承认。。。我被你的代码迷住了^^^完全^^你最终能把代码分割成更多的方法来指出你在做什么吗?或者你可以提供一些注释来解释你在这个或那个代码块中做了什么?我真的不想冒犯你,但即使读了两三遍,我还是不明白^^另一点是,这个问题有很多访客,但没有人敢回答,可能是因为上面的原因-没有冒犯,真的!!问题是,
decimalOfInput
初始值设定项给它一个整数除以8的值。将其乘以16得到一个整数,因此不放入句点的测试成功。我无法告诉你如何修复它,因为像@MartinFrank一样,我不知道你想做什么。好的,我告诉你哪里出了问题^^
如果(tempDec==(int)tempDec)
将被访问!当它到达时,你调用
break当这种情况发生时,您将永远无法访问代码
hexOutput=hex+”。。。所以我想你只是删除了
break
-语句。。。但这只是一个幸运的猜测。。。如果我是你,我会尝试拆分字符串并使用
Integer.parseInt(str,16)(十六进制输入)或
整数.parseInt(str,2)(二进制输入)…为了使用parseInt策略,点后的字符串需要添加尾随零,以使其长度为4的倍数。如果将
Integer.parseInt(str,2)
应用于“011”,结果将是3,而不是6。好的,您使用的基数是多少?如果您使用的是32位表示,您能提供它吗?还是8比特?不要泄露秘密^^Thnx,但我认为如果二进制输入的分数部分超过四位数,这将不起作用。对于注释,可能您没有正确阅读我的代码,我的代码中也有注释,您不知怎么错过了。@martinfranker是的。。。我想你是对的。。。对不起,我误解了你。。。对不起,给你添麻烦了。。。
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);    

    }
}