我如何转换非常大的十进制数为二进制在Java [英] How Can I Convert Very Large Decimal Numbers to Binary In Java

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

问题描述

比如说,我怎么能够转换 2 ^ 60 12345678901234567890123456789012345678901234567890 二进制?
基本上,这是太大的数字重新present 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.

编辑:我将作出一个类,将能够重新太大的present号。我只是有一个很难搞清楚我们如何将十进制转换为二进制。

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.

推荐答案

试试这个:

new BigDecimal("12345678901234567890123456789012345678901234567890").toString(2);


编辑:

有关制作一个大数字类,您可能想看看<一个href=\"http://stackoverflow.com/questions/5318068/very-large-numbers-in-java-without-using-java-math-biginteger/5318896#5318896\">my张贴这个一周前。啊,问题是由你,没关系。

For making a big-number class, you may want to have a look at my post about this a week ago. Ah, the question was by you, never mind.

在原则上不同数量的系统之间的转换是重复师,余数,乘,添加操作。让我们来看一个例子:

The conversion between different number systems in principle is a repeated "division, remainder, multiply, add" operation. Let's look at an example:

我们希望123从十进制转换为基地3号。我们该怎么办?

We want to convert 123 from decimal to a base 3 number. What do we do?


  1. 取余数模3 - prePEND这一数字到结果

  2. 除以3。

  3. 如果数目大于0,则继续使用该号码,在步骤1

因此​​,它看起来是这样的:

So it looks like this:


  • 123%3 == 0 。 ==>最后一个数字是 0

  • 三分之一百二十三== 41

  • 41%3 == 2 ==>倒数第二位的是 2

  • 41/3 = = 13

  • 13%3 == 1 ==>第三个数字是 1

  • 13/3 = = 4

  • 4%3 == 1 ==>第四个数字是 1 。

  • 4/3 == 1

  • 1%3 == 1 ==>第五个数字是 1

  • 123 % 3 == 0. ==> The last digit is 0.
  • 123 / 3 == 41.
  • 41 % 3 == 2 ==> The second last digit is 2.
  • 41 / 3 == 13
  • 13 % 3 == 1 ==> The third digit is 1.
  • 13 / 3 == 4
  • 4 % 3 == 1 ==> The fourth digit is 1 again.
  • 4 / 3 == 1
  • 1 % 3 == 1 ==> The fifth digit is 1.

因此​​,我们有 11120 作为结果。

So, we have 11120 as the result.

问题是,为此,你需要通过3十进制格式已经某种分裂,这通常不是这样的,如果你不实现您在基于十进制格式的数字(像我在做回答你上面链接的最后一个问题)。

The problem is that for this you need to have already some kind of division by 3 in decimal format, which is usually not the case if you don't implement your number in a decimal-based format (like I did in the answer to your last question linked above).

但它适用于从内部数字格式转换为任何外部格式。

But it works for converting from your internal number format to any external format.

那么,让我们来看看,我们会怎么做反向计算,从 11120 (基地3)等效的十进制数。 (3基地这里是对于任意基数占位符,10个基本的占位符内部基数)原则上,这个数字可以写成这样:

So, let's look at how we would do the inverse calculation, from 11120 (base 3) to its decimal equivalent. (Base 3 is here the placeholder for an arbitrary radix, Base 10 the placeholder for your internal radix.) In principle, this number can be written as this:

1 * 3^4 + 1 * 3^3 + 1*3^2 + 2*3^1 + 0*3^0

有一个更好的方法(更快来计算)是这样的:

A better way (faster to calculate) is this:

((((1 * 3) + 1 )*3 + 1 )*3 + 2)*3 + 0
    1
        3
             4
                12
                    13
                        39
                            41
                              123
                                  123

(这被称为<青霉>霍纳方案的,通常用于计算多项式的值。)

(This is known as Horner scheme, normally used for calculating values of polynomials.)

您可以在您所用的一些方案实现这一点,如果你知道如何重新present输入基数(和数字)的目标系统。

You can implement this in the number scheme you are implementing, if you know how to represent the input radix (and the digits) in your target system.

(<我一个href=\"https://github.com/ePaul/stackoverflow-examples/commit/2a029557f8b2ea6e2970c673e2d7451eac7ffb0e\"相对=nofollow>只是增加这样一算我DecimalBigInt类,但您可能希望直接在您的内部数据结构做了计算,而不是创建您BigNumber类的新对象(甚至两个)的每十进制数字被输入。)

(I just added such a calculation to my DecimalBigInt class, but you may want to do the calculations directly in your internal data structure instead of creating a new object (or even two) of your BigNumber class for every decimal digit to be input.)

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

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