Home Java Beginners Convert integer type data into binary, octal and hexadecimal
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Convert integer type data into binary, octal and hexadecimal

Advertisement
In this section, you will learn how to convert an integer type data into binary, octal and hexadecimal.

Convert integer type data into binary, octal and hexadecimal

     

In this section, you will learn how to convert an integer type data into binary, octal and hexadecimal. The  java.lang package provides the facility for converting the data integer into binary, octal, and hexadecimal. The following program helps you to convert into these. toBinaryString() method converts integer type data into binary, toHexString() method converts integer type into hexadecimal and toOctalString() method converts integer into octal type data. 

Description of code:

toBinaryString(int in):
This is the method that takes an integer type value and returns the string representation of unsigned integer value that will represent the data in binary (base 2).

toHexString(int in):
This is the method that takes an integer type value and converts in hexadecimal or in an unsigned integer (base 16).

toOctalString(int in):
This is the method that takes an integer type value and converts it into octal type data. The octal type data has base 8.

Here is the code of program:

public class DataConversion{
  public static void main(String[] args) {
  System.out.println("Data types conversion example!");
  int in = 50;
  System.out.println("Integer: " + in);
  //integer to binary
  String by = Integer.toBinaryString(in);
  System.out.println("Byte: " + by);
  //integer to hexadecimal
  String hex = Integer.toHexString(in);
  System.out.println("Hexa decimal: " + hex);
  //integer to octal
  String oct = Integer.toOctalString(in);
  System.out.println("Octal: " + oct);
  }
}

Download this example.

Output of program:

C:\vinod\Math_package>javac DataConversion.java

C:\vinod\Math_package>java DataConversion
Data types conversion example!
Integer: 50
Byte: 110010
Hexa decimal: 32
Octal: 62
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 8, 2007

Ask Questions?    Discuss: Convert integer type data into binary, octal and hexadecimal   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Muhammad Yousif
June 6, 2013
Java

it a good
dinesh
November 5, 2011
thanks

i have searching this method on long day.....however this is useful method thank you........
DMCA.com