Converting an Integer to Binary, Octal, and Hexadecimal

int intValue = 24;
											String binaryStr = Integer.toBinaryString(intValue);
											String octalStr = Integer.toOctalString(intValue);
											String hexStr = Integer.toHexString(intValue);

Using the Integer class, it is easy to convert an integer value from decimal to binary, octal, or hexadecimal. The relevant static methods on the Integer class are the toBinaryString(), toOctalString(), and toHexString() methods. In this phrase, we use each of these methods—each time passing in an integer value and getting a String returned that contains the integer in binary, octal, and hex format, respectively.

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.