Java Data Type How to - Convert an integer into binary, hexadecimal, and octal








Question

We would like to know how to convert an integer into binary, hexadecimal, and octal.

Answer

public class Main {
  public static void main(String args[]) {
    int num = 19648;
//from  ww  w  .  j  a  v a  2s.  c om
    System.out.println(num + " in binary: " + Integer.toBinaryString(num));

    System.out.println(num + " in octal: " + Integer.toOctalString(num));

    System.out.println(num + " in hexadecimal: " + Integer.toHexString(num));
  }
}

The code above generates the following result.