問題描述
我需要將一個(gè)非常大的二進(jìn)制值轉(zhuǎn)換為其十進(jìn)制等效值.因?yàn)樗且粋€(gè)大整數(shù),所以我使用的是 BigInteger.那么如何將此二進(jìn)制數(shù)轉(zhuǎn)換為 BigInteger?
I needed to convert a very big binary value into its decimal equivalent. As it is a big integer I was using BigInteger. So how do I convert this binary number to a BigInteger?
推薦答案
如果你有二進(jìn)制數(shù)的 String
表示,將它提供給這個(gè)重載的 BigInteger
構(gòu)造函數(shù)以創(chuàng)建一個(gè)實(shí)例:
If you have the String
representation of your binary number, provide it to this overloaded BigInteger
constructor to create an instance:
BigInteger(String val, int radix);
在你的情況下,基數(shù)顯然是 2,即你可以使用這樣的東西:
In your case, radix is clearly 2, i.e. you can use something like this:
BigInteger yourNumber = new BigInteger("101000101110...1010", 2);
這篇關(guān)于如何在 Java 中將二進(jìn)制數(shù)轉(zhuǎn)換為 BigInteger?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!