Decimal to Binary conversion in JAVA

This tutorial would help in learning the following: (Decimal to binary conversion in Java)

  • Conversion of numbers from decimal-system (using 0 to 9) to binary-system (only 1 and 0)
  • Conversion of characters into their respective ASCII codes
  • Code for Decimal to Binary conversion in JAVA

The decimal number system uses all the 10 digits (0 to 9) to represent a number while the binary number system uses two digits (1 and 0). The following algorithm is used to convert a decimal-number into a binary-number:

let the number be 63

  1. divide the number by 2 (integer division) and note down the remainder and keep the quotient
    63/2: quotient -> 31, remainder -> 1
  2. repeat step 1 on the quotient till there comes another quotient which is 0, note down the remainders obtained in right-to-left fashion
    31/2: quotient -> 15, remainder -> 1; remainder-list -> 11
    15/2: quotient -> 7, remainder -> 1; remainder-list -> 111
    7/2: quotient -> 3, remainder -> 1; remainder-list -> 1111
    3/2: quotient -> 1, remainder -> 1; remainder-list -> 11111
    1/2: quotient -> 0, remainder -> 1; remainder-list -> 111111
  3. the remainders, placed in the right-to-left fashion form the binary representation
    (63)10~ (111111)2

Each character used in computing is assigned a special ASCII code (decimal-number-system); hence, to convert a character into binary-form, its ASCII code can be obtained and it can be converted
Note, there can be different encoding systems on different machines.

Convert Decimal to binary in Java

public class BinaryConversion
{
String string;
int number;
BinaryConversion(String x,int y)//parameterized-constructor
{
    string=x;
    number=y;
}
BinaryConversion()//default constructor
{
    string="Code Speedy";
    number=63;
}
public static void main(String args[])
{
    BinaryConversion ob;
    try{//considering the command line argument of the user as the string to be converted to binary format
        ob=new BinaryConversion(args[0],Integer.parseInt(args[1]));
    }//going with the default string in case of any exceptions with the command line argument
    catch(Exception e){ob=new BinaryConversion();};
    System.out.println("The binary form of "+ob.number+" is: "+ob.binary(ob.number));
    System.out.println("The binary form of "+ob.string+" is: ");
    ob.stringToBinary(ob.string);
}
void stringToBinary(String code)//converts a line to binary code
 {
     int c;
     for(int i=0;i<code.length();i++){
         c=(int)(code.charAt(i));//converting the character to its ASCII value
         System.out.print(binary(c)+".");
     }
 }
int binary(int x)//converts an integer to its equivalent binary form
 {
     if(x<=1)return x;
     else return (x%2) + 10*binary(x/2);
 }
}//class ends

Output: (without command line argument)

The binary form of 63 is: 111111
The binary form of Code Speedy is: 
1000011.1101111.1100100.1100101.100000.1010011.1110000.1100101.1100101.1100100.1111001.

Summary:

int binary(int):

  • a recursive approach is taken
  • a remainder (x%2) is taken at each call and added next to the remainder of the next call
  • multiplication with 10 is done to ensure that the addition is done in right-to-left fashion and not simple summation

void stringToBinary(String):

  • a character is extracted, at a time, from the String ‘code’ and its integer-code (decimal system) is stored in ‘c’.
  • binary(int) is called and ‘c’ is passed to print the equivalent binary code.

We hope you have learned decimal to binary conversion in Java.

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *