Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8366927

Long.parseLong Cannot Parse A Valid Binary String

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Windows 11, Java 21.0.7

      A DESCRIPTION OF THE PROBLEM :
      When attempting to parse the following binary string:

      1000000100000001100000100000001010000011000000111000010000000100

      I get a NumberFormatException despite this being a valid 64-bit number.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See test case code.

      ACTUAL -
      Exception in thread "main" java.lang.NumberFormatException: For input string: "1000000100000001100000100000001010000011000000111000010000000100" under radix 2
      at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
      at java.base/java.lang.Long.parseLong(Long.java:709)
      at sandbox.Sandbox.main(Sandbox.java:11)


      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) throws Exception
      {
      long x = -9_150_890_020_540_546_044l;
      String str = Long.toBinaryString(x);
      long y = Long.parseLong(str, 2); //throws exception
      System.out.println(x == y); //should be the same
      }
      ---------- END SOURCE ----------

        1. Test.java
          0.3 kB
          Patricia Tavares

            pmtavare Patricia Tavares
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: