dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Converting Base 10 Numbers to Binary Numbers

Should you ever need to convert base 10 numbers to binary numbers, Java provides and easy mechanism.


advertisement

Programming needs vary. You may have a requirement to convert a Base 10 value to binary as part of a complex logic. Java has an easy mechanism to achieve this.

public class Base10ToBinary
{
   public static void main(String args[])
   {
      Base10ToBinary base10ToBinary = new Base10ToBinary();
      base10ToBinary.proceed();
   }
   
   private void proceed()
   {
      int num = 10;

      String binaryNum = Integer.toString(num, 2); 
        System.out.println("Binary value of " + num + " : " + binaryNum); 
   }
}

/*

Expected output:

[root@mypc]# java Base10ToBinary
Binary value of 10 : 1010

*/

 

Visit the DevX Tip Bank

 



   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Thanks for your registration, follow us on our social networks to keep up-to-date