How to Convert Int to Binary in Java

How to represent binary numbers in java?  or
How to convert int to binary in java?

A binary number is a string of 1′s and 0′s such as 10101101. To represent binary numbers or to convert an integer into a binary number in java, wrapper class Integer can be used. As specified in java docs, toBinaryString method of Integer class, returns a string representation of the integer argument as an unsigned integer in base 2.

Below is the code snippet to demonstrate the conversion of an int into a binary:

Int to Binary in Java

public class ConvertToBinary {

public static void main(String[] args) {

int intNum = 10;

String strBin = Integer.toBinaryString(intNum);

System.out.println(“Binary representation of ” + intNum + “is ” + strBin);

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s