【问题标题】:Any datatype for integers longer than a Long?任何长于 Long 的整数的数据类型?
【发布时间】:2021-01-06 16:08:19
【问题描述】:

我想将二进制转换为整数,将其乘以 17,然后再将其转换回二进制。这是我的代码:

Scanner scan = new Scanner(System.in);
String n = scan.nextLine();
long j = Long.parseLong(n, 2);
j = j * 17;
System.out.println(Long.toBinaryString(j));

我最初将 j 设为 int,但在获得更大的测试用例后更改了它:

10001111110001000101000001000100111100110101100011000011011001111000100110110000110101110101100001001100010111000101000100010010011000000010010

它有一个 NumberFormatException,这是有道理的,因为 long 只能存储有限数量的数字,那么对于非常长的整数是否有任何数据类型?

【问题讨论】:

  • 您是在问为什么会发生异常?或者如何解决这个数字大小的问题?
  • @user202729 哦,我知道为什么会发生异常,我会尽快编辑我的帖子。我需要有关数字大小的帮助。
  • 无论如何请看stackoverflow.com/questions/17833463/…;但是,如果这是家庭作业,您应该自己实现它
  • 如果问题是二进制数太大,使用BigInteger。 (您应该能够找到 javadocs 以了解如何操作。提示:使用 google ....)
  • 是否必须将中间结果具体化为long?您可以考虑添加两个二进制字符串:(b + "0000") + ("0000" + b)。我花了几分钟才想出一个解决方案,我使用“long”方法只测试了较小的字符串。

标签: java long-integer numberformatexception


【解决方案1】:

您是否尝试过 BigInteger 或 BigDecimal。

https://www.baeldung.com/java-bigdecimal-biginteger

这两种类型专门用于要求数字具有较大或任意范围(例如某个值 > 或 = 到 1x10^307 且小于 1x10^-307)的情况

public void whenBigDecimalCreated_thenValueMatches() {
    BigDecimal bdFromString = new BigDecimal("0.1");
    BigDecimal bdFromCharArray = new BigDecimal(new char[] {'3','.','1','6','1','5'});
    BigDecimal bdlFromInt = new BigDecimal(42);
    BigDecimal bdFromLong = new BigDecimal(123412345678901L);
    BigInteger bigInteger = BigInteger.probablePrime(100, new Random());
    BigDecimal bdFromBigInteger = new BigDecimal(bigInteger);
        
    assertEquals("0.1",bdFromString.toString());
    assertEquals("3.1615",bdFromCharArray.toString());
    assertEquals("42",bdlFromInt.toString());
    assertEquals("123412345678901",bdFromLong.toString());
    assertEquals(bigInteger.toString(),bdFromBigInteger.toString());
}

这应该对你有帮助。

【讨论】:

    【解决方案2】:

    当计算超出long的容量时使用BigInteger,例如

    String input = "10001111110001000101000001000100111100110101100011000011011001111000100110110000110101110101100001001100010111000101000100010010011000000010010";
    
    // parse binary string
    BigInteger num1 = new BigInteger(input, 2);
    
    // multiply by 17
    BigInteger num2 = num1.multiply(BigInteger.valueOf(17));
    
    // format as binary string
    String output = num2.toString(2);
    
    System.out.println(output);
    

    输出

    100110001100000010010101010010010100001010001110010011111001111000000010010010111110010011001101110100010010001000010110001000111000011000100110010
    

    【讨论】:

      【解决方案3】:

      例如:

      import org.junit.Test;
      
      import java.util.function.Function;
      
      public class BinTest
      {
          String binNo1 = "100011111100010001010000010001001111001101011000";
          String binNo2 = "10001111110001000101000001000100111100110101100011000011011001111000100110110000110101110101100001001100010111000101000100010010011000000010010";
      
          @Test
          public void testIt()
          {
              //System.out.println( bin17A( binNo1 ) );
              System.out.println( bin17S( binNo2 ) );
          }
      
      
      
          public static String bin17S( String bin )
          {
              // * 16
              String bin16 = bin + "0000";
              String bin01 = "0000" + bin;
              StringBuilder result = new StringBuilder();
      
              Function<Character, Integer> parser = c -> (c == '1') ? 1 : 0;
      
              int carry = 0;
              for ( int i = bin16.length() - 1; i >= 0; i-- )
              {
                  int value = parser.apply( bin16.charAt( i ) )
                          + parser.apply( bin01.charAt( i ) )
                          + carry;
      
                  carry = value / 2;
      
                  result.insert(0, value % 2 );
              }
              while (carry > 0)
              {
                  result.insert(0,carry % 2 );
                  carry = carry / 2;
              }
              return result.toString();
          }
      
      
          public static String bin17A( String bin )
          {
              long j = Long.parseLong( bin, 2 );
              j = j * 17;
              return Long.toBinaryString( j );
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        相关资源
        最近更新 更多