Mathematically calculating decimal to binary (java) -


I have a problem with my algorithm to change a decimal number without using parsing in the binary number, This is the related section code:

  public static zero dtb () {System.out.print ("\ nDenary number to change (less than 255):"); // eight bit conversion intron num = sc.nextInt (); Int decVal = 128; Int saveNum = num; // save point for first input int [arr]; Arr = new int [8]; {If (num - decVal> 0) {// Binary decimal decimal number = num - decVal; Arr [x] = 1; } And arrival [x] = 0; DecVal = decVal / 2; X ++; } While (DCVil! = 1); System.out.print ("\" "+ saveNum +" binary value is: "+ arr [0] + ARR [1] + ARR [2] + ARR [3] + ARR [4] + ARR [5] + AR [6] + Arrivals [7]);}   

x is declared initially, therefore out of sight. I also know where the error is , But I can not work properly to do this, so ask for help, I'm also quite new, so any help on other methods would be appreciated. For example, output = (input = 42) 00101000

You need to compare it Is num - decVal or equal to 0 because you have & gt; , Then it is equal to 0 and you are not forgeting 1 bit.

  if (num - decVal> = 0) {  

In addition, you will need a loop, while decVal is not equal to 0 , etc. Instead 1 , or you'll miss the last bit odd number.

 } while (decVal! = 0);   

Output with changes:

  Binary value of 42 is: 00101010 43 Binary value is: 00101011    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -