如何在 Java 中将非常大的十进制数转换为二进制数

时间:2022-11-25
本文介绍了如何在 Java 中将非常大的十进制数转换为二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

例如,如何将 2^6012345678901234567890123456789012345678901234567890 转换为二进制?基本上,数字太大而无法用 Java 表示.

For instance, How would I be able to convert 2^60 or 12345678901234567890123456789012345678901234567890 to binary? Basically, numbers that are too large to represent in Java.

我将创建一个能够表示太大数字的类.我只是很难弄清楚如何将十进制转换为二进制.

I will be making a class that will be able to represent number that are too large. I'm just having a hard time figuring our how to convert decimal to binary.

Edit2:另外,我不允许使用 BigDecimal、BigInteger 或任何其他库,抱歉之前没有指定.

And also, I am not allowed to use BigDecimal, BigInteger, or any other library, sorry for not specifying earlier.

推荐答案

这是一个quik&dirty(非常非常非常脏)的代码:

Here is a quik&dirty (very very very dirty) code:

public class BigDec2Bin {

    public static int[] string2arrayReversed( String s )
    {
        char a[] = s.toCharArray();
        int  b[] = new int[ s.length() ];
        for( int i = 0; i < a.length; i++ )
        {
            b[a.length-1-i] = a[i] - 48;
        }
        return b;
    }

    // adds two binary numbers represented as strings
    public static String add( String s1, String s2 )
    {
        String result = "", stmp;
        int[] a1, a2;
        int ctmp, mark = 0;

        // a1 should be the longer one
        a1 = string2arrayReversed( ( s1.length() > s2.length() ? s1 : s2 ) );
        a2 = string2arrayReversed( ( s1.length() < s2.length() ? s1 : s2 ) );

        for( int i = 0; i < a1.length; i++ )
        {
            ctmp = a1[i] + ( i < a2.length ? a2[i] : 0 ) + mark;

            switch( ctmp )
            {
                default:
                case 0:
                    stmp = "0";
                    mark = 0;
                    break;
                case 1:
                    stmp = "1";
                    mark = 0;
                    break;
                case 2:
                    stmp = "0";
                    mark = 1;
                    break;
                case 3:
                    stmp = "1";
                    mark = 1;
                    break;
            }

            result = stmp + result;
        }

        if( mark > 0 ) { result = "1" + result; }

        return result;
    }

    public static String dec2bin( String s )
    {
        String result = "";

        for( int i = 0; i < s.length() ; i++ )
        {
            result = add( result + "0", result + "000" );
            result = add( result, Integer.toBinaryString( s.charAt(i) - 48 ) );
        }

        return result;
    }

    public static void main( String[] args )
    {
        String dec = "12345"; // should be 11000000111001
        System.out.println( "dec2bin( " + dec + " ) = " + dec2bin( dec ) );

        dec = "12345678901234567890123456789012345678901234567890";
        System.out.println( "dec2bin( " + dec + " ) = " + dec2bin( dec ) );
    }

}

<小时>

输出:

dec2bin(12345) = 011000000111001

dec2bin( 12345 ) = 011000000111001

dec2bin(12345678901234567890123456789012345678901234567890) =10000111001001111111011000110110100110101010111110000011110010100001010100000010011001110100011110101111100011000111111100011001011011001110001111110000101011010010

dec2bin( 12345678901234567890123456789012345678901234567890 ) = 10000111001001111111011000110110100110101010111110000011110010100001010100000010011001110100011110101111100011000111111100011001011011001110001111110000101011010010

<小时>

我的主要想法是始终使用字符串.


My main idea is to use always strings.

add - 方法将两个二进制数相加,表示为字符串
dec2bin -方法是神奇的地方.

add -method adds two binary numbers which are represented as strings
dec2bin -method is where the magic happens.

请允许我解释一下:

result = add( result + "0", result + "000" );

是任何给定数字乘以 10 的计算.

is a calculation to multiply any given number by 10.

将二进制数乘以 10 与将数字相加相同:

Multiplying a binary number by 10 is the same as adding the number with shifts:

x*10 <=> x<<1 + x<<3

x*10 <=> x<<1 + x<<3

result = add( result, Integer.toBinaryString( s.charAt(i) - 48 ) );

只需在结果字符串上添加下一个数字(从左到右)

just adds a the next digit (from left to right) on the result string

基本上我正在做的是例如 1234:
0*10 + 1 = 1
1*10 + 2 = 12
12*10 + 3 = 123
123*10 + 4 = 1234

Basicly what I'm doing is for example with 1234:
0*10 + 1 = 1
1*10 + 2 = 12
12*10 + 3 = 123
123*10 + 4 = 1234

但只能以二进制形式(表示为字符串).

but only in binary (represented as strings).

我希望我能帮上忙,并为我的英语不好感到抱歉.

I hope i could help and sorry for my bad english.

这篇关于如何在 Java 中将非常大的十进制数转换为二进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:检查整数中是否只设置了一个位(无论其位置如何) 下一篇:将整数转换为零填充的二进制字符串

相关文章

最新文章