如何在 Java 中将二进制数转换为 BigInteger? [英] How do you convert A binary number to a BigInteger in Java?

查看:35
本文介绍了如何在 Java 中将二进制数转换为 BigInteger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个非常大的二进制值转换为其十进制等效值.因为它是一个大整数,所以我使用的是 BigInteger.那么如何将此二进制数转换为 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?

推荐答案

如果你有二进制数的 String 表示,将它提供给这个重载的 BigInteger 构造函数以创建一个实例:

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

在你的情况下,基数显然是 2,即你可以使用这样的东西:

In your case, radix is clearly 2, i.e. you can use something like this:

BigInteger yourNumber = new BigInteger("101000101110...1010", 2);

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆