久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Java 中將非常大的十進制數轉換為二進制

How Can I Convert Very Large Decimal Numbers to Binary In Java(如何在 Java 中將非常大的十進制數轉換為二進制數)
本文介紹了如何在 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 中將非常大的十進制數轉換為二進制數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 一区日韩 | 一区视频 | 亚洲www| 国产精品区一区二区三 | 成人久久久 | 国产一区91精品张津瑜 | 黄色香蕉视频在线观看 | 精品欧美一区二区三区免费观看 | 欧美成人精品在线 | 视频在线一区 | 久草免费视 | 久久久久国产一区二区三区 | 午夜精品视频一区 | 欧美黑人一级爽快片淫片高清 | 在线欧美一区二区 | 91久久久久久久久久久久久 | 久久se精品一区精品二区 | 超碰超碰 | 国产美女精品视频免费观看 | 国产精品视频久久 | 一级高清免费毛片 | 精品国产精品国产偷麻豆 | 国产精品美女久久久久久免费 | 久久综合九九 | 福利精品 | 综合色久| 香蕉视频91 | 亚洲精选久久 | 一级欧美一级日韩片 | 国产福利在线 | 男女羞羞视频在线看 | 日韩欧美一级片 | 亚洲色图插插插 | av在线免费网站 | 色综合视频 | 亚洲精品一区二区网址 | 在线欧美视频 | 欧美日韩在线免费观看 | 91天堂网 | 国产视频二区 | 成人av播放 |