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

assign int start with 0 result different number

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      Hi,
      I was doing a simple assignment with a binary number convert to decimal. I am not sure is it a bug. I am working with an IDE. When I assign an int of value 01010001, the value in the function becomes 266241. And when I assign an int in main function with 00151, it becomes 105 as shown in my IDE. I tried to look for an explanation. But it is not in the primitive data type document.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      I am using JDK11.0.4
      assign an int variable with any numbers start with 0 and follow by any number from 0-9.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      00151 should be 151. 01151 should be 1151 02151 should be 2151
      ACTUAL -
      00151 get stores 105, 01151 get stores as 617, 02151 get stores as 1129

      ---------- BEGIN SOURCE ----------
      public class Bin2Dec {
      //intput a binary number out put the 10 base

      public static int B2D (int num)
      {
      int result=0;
      int base =1;
      while(num >0)
      {
      result+= num%10*base;
      base *=2;
      num/=10;
      }

      return result;

      }

      public static void main(String[] args) {
      // int[] array = { 2, 7, 11, 15 };
      // int[] indexs = new int[2];
      // indexs = twoSum(array, 9);
      // System.out.println(indexs[0]);
      // System.out.println(indexs[1]);

      B2D(01010001);
      int num = 00151;
      int num1 = 01151;
      int num2 = 02151;
      int num3 = 03151;
      int num4 = 04151;
      int num5 = 05151;
      int num6 = 06151;
      int num7 = 07151;


      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      just delete the 0's in front of the numebr

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              tongwan Andrew Wang
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: