Number to Binary, Hexa, Octa String

Some times we need the binary, hexadecimal or octal representation of the integer. In java you can easily have this with Integer class in one line.

Wrapper class of int, Integer has static methods to achieve this easily. Integer contains static methods like toBinaryString, toHexString and toOctalString to convert number to specific number system representation,but note that these methods return String.

Please consider following example,

public class NumberToBHO {

	public static void main(String[] args) {
		int number = 10;


In above code we will have following output,


Kindly note that all these static methods return the String representation of the number in specific number system. As we can see for number 10 we have a in hexadecimal. Moreover , you can not directly parse this back to the String.So, Integer.parseInt(1010) will obviously won’t give you 10 that is quite understandable.

Further more for long types you can have same static methods in wrapper of long, Long. Check out the same,

public class NumberToBHO {

	public static void main(String[] args) {
		long numberL = 1000000000;


and for above code we will have the following output,


Moreover you can directly pass the int to Long methods as long can handle int but vice versa is not valid.On the other hand for the floating point number types like Double and Float we have only toHexString methods. There is an indirect way to convert floating point number to binary String representation, we will discuss that in upcoming blog posts.

One thought on “Number to Binary, Hexa, Octa String

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s