Jump to content

Decimal To Binary Converter

- - - - -

  • Please log in to reply
10 replies to this topic

#1
Kushtrim

Kushtrim

    Newbie

  • Members
  • PipPip
  • 14 posts
Hello :)
This is my very first tutorial on this forum :lol:
Converting Decimal to Binary is quite simple. This is how we do it :


/**

 * Converts Decimal To Binary

 * 

 * @author Kushtrim

 * @version 14.12.2011

 */

import javax.swing.*;

public class DecimalToBinary

{

     public static void main(String[] args) 

  {  

      long d = new Long(JOptionPane.showInputDialog("Write the number in decimal form: ")).longValue(); 

      String binary="";

          for ( long decimal = d ; decimal > 0 ; decimal/=2 )

          {

           binary = decimal%2 + binary  ;

          }

      JOptionPane.showMessageDialog(null,"Binary number:\n " + binary);

  }

  

    

}

The logic behind this is really simple, u just got to know a little math.,
To convert a number from decimal form ( base-ten , u know the system we normally use), eg. 4, u divide it by 2, and you get a reminder 0 or 1 ( in our case 4:2=2, remainder 0) ... We repeat this process(loop) until we get the quotient 0 (the remainder of 1:2=0 will be 1).
Then we read it from bottom to top ( from the last remainder).

Our example:
4:2=2 remainder 0
2:2=1 remainder 0
1:2=0 remainder 1

Decimal 4 = Binary 100

Feel free to ask if you didn't understand it:)

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Note that there are built in methods in Java for this;)
int decimal=415;
System.out.println(decimal + " in binary -> " + Integer.toString(decimal, 2));

String binary = "010110111";
System.out.println(binary + " in decimal -> " + Integer.parseInt(binary, 2));


#3
Kushtrim

Kushtrim

    Newbie

  • Members
  • PipPip
  • 14 posts
:w00t: Thank you very much @wim DC.:thumbup1:
I wasn't aware that there are methods on the Integer class that could do this much easily
This was an exercise in the book I'm reading, on the chapter about loops ... so I tried to achieve it by using for loops :)

Thanks again:)

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
This is still a good little tutorial though. It teaches how to reach the binary equivalent of decimal numbers computationally.
Some people, like me, enjoy knowing about the "backbone" of certain algorithms and not just:

var answer = doTheWorkForMeBro();

print(answer);

I'm not saying that available functions aren't useful though! :p

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Assembly language is nice, it usually doesn't have any built-in functions, unless you include or import libraries :D . That means that you have all (or almost all) the rights there are to write your own functions :thumbup1: .

#6
J4keVC

J4keVC

    Newbie

  • Members
  • PipPip
  • 10 posts
Very usefull thx, but how about E.g 9?

=> 9:2 => 0 remainder: 1
=> 4.5:2 => ??

or am I doing wrong? cause it'll only work if you take 2,4,6,8, ...

Thanx Already ! ;)

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
You floor - or round down - the number in the integer world.

9 / 2 = 4 and 9 % 2 = 1
4 / 2 = 2 and 4 % 2 = 0
...

Or a non-2 quotient:
14 / 3 = 4 and 14 % 3 = 2
4 / 3 = 1 and 4 % 3 = 1
1 / 3 = 0 and 1 % 3 = 1

#8
Paulo_Jorge

Paulo_Jorge

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
nice , can u share a code to convert from binary to decimal ! !

#9
Kushtrim

Kushtrim

    Newbie

  • Members
  • PipPip
  • 14 posts

Paulo_Jorge said:

nice , can u share a code to convert from binary to decimal ! !

sure :)

import javax.swing.*;

public class BinaryToDec

{

   public static void main (String args[])

   {

       String binary = JOptionPane.showInputDialog("Binary number:"); // get the input

       int decimal=0;

       int k=0;

       for(int i=binary.length()-1; i>=0  ; i-- )   // 

       { 

           if ( binary.charAt(i) == '1' )

           { decimal+= Math.pow(2,k);} 

           else if ( binary.charAt(i) == '0')

           { //do nothing

            }

            else {

                JOptionPane.showMessageDialog(null,"Error: A binary number must contain only 0 and 1");

                System.exit(0);

            }

           k++;

         

        }

        JOptionPane.showMessageDialog(null,"Decimal value: " + decimal);

    }

    }



#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

[URL="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I'm Feeling Lucky"][COLOR=#003399]String[/COLOR][/URL] number [COLOR=#339933]=[/COLOR] [COLOR=#0000FF]"101011"[/COLOR][COLOR=#339933];[/COLOR]                
[B]int[/B] result [COLOR=#339933]=[/COLOR] [URL="http://www.google.com/search?hl=en&q=allinurl%3Ainteger+java.sun.com&btnI=I'm Feeling Lucky"][COLOR=#003399]Integer[/COLOR][/URL].[COLOR=#006633]parseInt[/COLOR][COLOR=#009900]([/COLOR]number, [COLOR=#CC66CC]2[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]                
[URL="http://www.google.com/search?hl=en&q=allinurl%3Asystem+java.sun.com&btnI=I'm Feeling Lucky"][COLOR=#003399]System[/COLOR][/URL].[COLOR=#006633]out[/COLOR].[COLOR=#006633]println[/COLOR][COLOR=#009900]([/COLOR]number [COLOR=#339933]+[/COLOR] [COLOR=#0000FF]" --> "[/COLOR] [COLOR=#339933]+[/COLOR] result[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]  


#11
Paulo_Jorge

Paulo_Jorge

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
thanks, !! I'm going to test it - just to confirm again !!

And then, aplly this to a java form !! With buttons and textboxes , really cool !!

Thanksa again , ;)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users