2 ways to convert a Number to Binary using Java (Custom function included)


Converting an Integer to Binary format is a common problem which is taught in Computer Science. All computers read and understand Binary number format, which represents all characters as a combination of 0s and 1s.

In order to convert an Integer to Binary, the method used is very simple. Since Binary Number system is based on 2 characters (0 and 1), we divide the Integer by 2 and store the result. We keep doing this till the number becomes 0.

If we have to write a program in Java, how do we do that? 

Let's check two ways to solve this problem in Java:

1. Using the Inbuilt function to convert to Binary

We can use the inbuilt function toBinaryString which makes the process really easy.

As per java documentation:

String java.lang.Integer.toBinaryString(int i)

Returns a string representation of the integer argument as anunsigned integer in base 2. 

Let's look at the implementation:

int number = 2256;
System.out.println("Using inbuilt function:"+Integer.toBinaryString(number));


Using inbuilt function:100011010000

2. Writing a custom function to convert Integer to Binary

If you are asked to write your own function to demonstrate that you understand the logic, its again an easy task. We'll keep on dividing the number and storing the remainder to be added as a binary. 

We can utilize the mod operator "%" to get the remainder. 

To get the next dividend, we'll divide the number by 2. We'll continue this process till the number becomes 0.

Let's check out the code:

package coding;

import org.apache.commons.lang3.StringUtils;

public class ConvertBinary {

	public static void main(String[] args) {
		int number = 2256;
		System.out.println("Using custom function:"+convertIntegerToBinary(number));
		System.out.println("Using inbuilt function:"+Integer.toBinaryString(number));

	 * Convert a Decimal number to Binary
	 * @author computengine.com
	 * @param num
	 * @return
	public static String convertIntegerToBinary(int num) {
		String binary = "";
		int remainder=0;
		//process the number till it becomes zero
		while(num>0) {
			//Use mod operator to store the remainder
			remainder = num%2;
			//divide by 2 to get the next dividend
			//store in binary string
			binary = binary + remainder;
		//reverse the string
		return StringUtils.reverse(binary);



Using custom function:100011010000

Using inbuilt function:100011010000

As you can see, both the Inbuilt function, and our own custom function are giving the same output. So we have arrived at the correct logic for solving this problem. 

Thanks for Reading the Article. If you have reached this far, we hope that the article was useful to you! Please Like/Share/Follow us on FacebookTwitterTumblr, LeetCode 


Popular posts from this blog

Calculate Your CTC Salary Hike Percentage - Online Calculator to find your New Salary

Find the Longest Streak of 1's in a Binary Array using Java Code

Java Program to Read a Properties file and return a Property Value

What is my IP Address - How to find your IP address on Local Network using Command Prompt, and External Network using JavaScript Code

Create a File logging Utility using Java Code