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


Tip of the Day
Language: Java
Expertise: Intermediate
Mar 10, 2020

Converting Base 10 Numbers to Binary Numbers

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

*/
M S Sridhar
 
Thanks for your registration, follow us on our social networks to keep up-to-date