繁体   English   中英

如何将大整数字符串转换为二进制?

[英]How to convert a large string of integers to binary?

[编辑]

抱歉,如果已经在其他线程中回答了这个问题。 在最近的一次采访中有人问我这个问题。 给定一大串整数(> 64bit),例如“ 163878712638127812737637876347236482”,如何将其转换为二进制? 不应使用与二进制相关的Java API或库来完成此操作。

从字符串创建一个BigInteger并调用toByteArray

布雷特的方法应该起作用。 但是,如果不允许使用任何外部库,可以将字符串视为数字数组并对其进行长除法。 因此,将数字重复除以2,直到剩下0或1。 您可能必须编写自己的方法来对数字数组进行除法(处理剩余数和余数),但是它应该具有相当好的运行时间。

下面是一个示例:假设您有字符串163878712638127812737637876347236482。将其转换为一个整数数组(如果需要考虑内存,则将其转换为short)。 用2进行长除法,将结果保存在单独的数组中,并跟踪余数:

int[] answer = new int[input.length]; //create the answer array
String binary = "";
public void convert(input, answer){
    for(int i=input.length-1;i<=0;i--) //you want to start from the left end
    {
        int temp = input[i]/2; //int division, disregard decimal.
        answer[i] = temp;
        int remainder = input[i] - temp*2; //will be 1 or 0, carry over to the next digit
        if(i>0) //check if we are not at the last digit
            input[i-1] += 10*remainder;
        else
            binary = remainder+binary; //add new bit to the left of the binary
    }
    if(answer_is_smaller_than_2) //check if we have 0 or 1 left, maybe just go through the answer array
       binary = answer[0]+binary;// add the last digit to binary string. It is generally ok to have a 0 in the beginning, but you can easily do a check and remove the 0
    else convert(answer, new int[answer.length]); //keep dividing

}

抱歉,递归不是很好。 我写了它。 有关将十进制转换为二进制的更多信息: http : //www.wikihow.com/Convert-from-Decimal-to-Binary希望对您有所帮助:)

我不得不考虑这个问题,这终于是我的解决方案。 我能够验证10位数字长数字字符串的输出,但不能验证程序中使用的字符串的输出。 如果有人可以验证并告诉我解决方案是否正确,那就太好了。 另外,该解决方案尚未优化,因此请随时提出更改建议:

public class BinaryString {
    String finArr = "";

    public static void main(String[] args) {

        String num = "37489237892374892734872398479827498238932787";
        BinaryString bs = new BinaryString();
        bs.getBinaryValue(num);
    }

    void getBinaryValue(String num) {

        String quo = getBin(num);

        if (!quo.equals("1")) {
            getBinaryValue(quo);
        } else {
            finArr = quo + finArr;
            System.out.println(" Final Binary Value=" + finArr);
            return;
        }
    }

    String getBin(String num) {

        int[] numArr = new int[num.length()];
        for (int i = 0; i < num.length(); i++) {
            numArr[i] = Character.getNumericValue(num.charAt(i));
        }
        int p = 0;
        String quo = "";
        int rem = numArr[0];

        for (int i = 0; i < numArr.length; i++) {

            p = rem / 2;
            if (p != 0) {
                quo = quo + p;
            } else if (p == 0 && i > 0) {
                quo = quo + p;
            }

            if ((i + 1) != numArr.length) {
                rem = numArr[i] % 2 * 10 + numArr[i + 1];
            } else {
                rem = rem % 2;
                break;
            }

        }

        finArr = Integer.toString(rem) + finArr;
        return quo;

    }

}

输出:

最终二进制值= 1101011100101101011110111111010001110101100001011011011110010001000101111111100101111101100001010010001101100101101001110011101101111011100110011

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM