Parsing and Formatting a Number into Binary, Octal, and Hexadecimal

int i = 1023; // Parse and format to binary i = Integer.parseInt("1111111111", 2); // 1023 String s = Integer.toString(i, 2); // 1111111111 // Parse and format to octal i = Integer.parseInt("1777", 8); // 1023 s = Integer.toString(i, 8); // 1777 // Parse and format to decimal i = Integer.parseInt("1023"); // 1023 s = Integer.toString(i); // 1023 // Parse and format to hexadecimal i = Integer.parseInt("3ff", 16); // 1023 s = Integer.toString(i, 16); // 3ff // Parse and format to arbitrary radix <= Character.MAX_RADIX int radix = 32; i = Integer.parseInt("vv", radix); // 1023 s = Integer.toString(i, radix); // vv

Comments

25 Feb 2010 - 5:55am by Anonymous (not verified)

these converted formats are only in strings... we cannot do any calculation using it... improve your answer...

27 Feb 2010 - 12:37am by patrick

These conversions are meant to be displayed, not for calculations. If you need to do calculations just perform them on the values while they're in integer form.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.