1

我尝试了以下代码:

import java.math.BigInteger;
import org.apache.commons.codec.binary.Base32;
import org.junit.Test;

public class Sandbox
{
  @Test
  public void testSomething() {
    String sInput = "GIYTINZUHAZTMNBX";

    BigInteger bb = new BigInteger(new Base32().decode(sInput));

    System.out.println("number = " + bb);
  }
}

这是输出:

number = 237025977136523702055991

使用这个网站在 base 32 之间进行转换,我得到的结果与实际输出不同。根据我从网站上得到的结果,这是我期望看到的结果:

expected output = 2147483647

知道为什么会这样吗?

编辑:

请原谅我故意尝试转换 2^31-1 造成混淆。

使用我之前链接到的转换网站,我更改了输入:

String sInput = "GE4DE===";

预期输出:

number = 182

实际输出:

number = 3225650
4

2 回答 2

1

你所做的是正确的......假设 Base32 字符串来自 Base32 编码的字节数组,你从调用 BigInteger.toByteArray().

BigInteger(byte[] val) does not really take an array of arbitrary bytes. It takes the byte[] representation of a BigInteger. Also, it assumes the most-significant byte is in val[0]).

于 2012-06-30T00:30:39.873 回答
0

If it's base-32 the X, Y, and Z shouldn't be there. Are you sure it isn't base-36?

于 2012-06-30T01:44:47.070 回答