int to Binary Octal Hexa Java


int to Binary Octal Hexa: Sometimes, it may be necessary to convert a decimal value into binary or octal or hexadecimal notation. Java comes with predefined methods to do the job with two styles.

Following methods are defined in java.lang.Integer class.

    public static java.lang.String toString(int, int) : converts first parameter int to second parameter int. Second parameter can be of 2, 8 or 16.
    public static java.lang.String toBinaryString(int): converts int to Binary form
    public static java.lang.String toOctalString(int) : converts int to Octal form
    public static java.lang.String toHexString(int)   : converts int to Hexa form
The above methods are used in the next program int to Binary Octal Hexa.
public class Demo
{
  public static void main(String args[])
  {                          // int value 100 is converted to binary  (2 indicates)
    System.out.println("\t\t1st style: Using toString(int, int) method");                                     
    String str1 = Integer.toString(100, 2);  
    System.out.println("Binay representation of 100 is " + str1);

                             // int value 100 is converted to octal (8 indicates)
    String str2 = Integer.toString(100, 8);
    System.out.println("Octal representation of 100 is " + str2);

                             // int value 100 is converted to hexa  (16 indicates)
    String str3 = Integer.toString(100, 16);  
    System.out.println("Hexadecimal representation of 100 is " + str3);

    System.out.println("\n\t\t2nd style: Using toXXXString(int) method");                                     

    String str4 = Integer.toBinaryString(100);  
    System.out.println("Binay representation of 100 is " + str4);

    String str5 = Integer.toOctalString(100);
    System.out.println("Octal representation of 100 is " + str5);

    String str6 = Integer.toHexString(100);
    System.out.println("Hexadecimal representation of 100 is " + str6);
  }
}

int to Binary Octal Hexa

All the methods are self-explanatory.

More indepth information on toString() method is available at toString Java.

3 thoughts on “int to Binary Octal Hexa Java”

  1. Just desire to say your article is as surprising. The clarity on
    your submit is just cool and i could assume you are knowledgeable
    in this subject. Well with your permission allow me
    to grab your RSS feed to stay up to date with drawing close post.
    Thank you a million and please carry on the rewarding
    work.

Leave a Comment

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