Java int convert to binary, hexadecimal, octal number

Introduction

The Integer and Long classes also provide the methods toBinaryString(), toHexString(), and toOctalString(), which convert a value into a binary, hexadecimal, or octal string, respectively.

The following program demonstrates binary, hexadecimal, and octal conversion:


/* Convert an integer into binary, hexadecimal,
   and octal.//from w  w  w.j  ava  2s .com
*/

public class Main {
  public static void main(String args[]) {
    int num = 19648;

    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));
  }
}



PreviousNext

Related