Parsing and Formatting a Big Integer into Binary, Octal, and Hexadecimal

BigInteger bi = new BigInteger("1023");

// Parse and format to binary
bi = new BigInteger("1111111111", 2); // 1023
String s = bi.toString(2);            // 1111111111

// Parse and format to octal
bi = new BigInteger("1777", 8);       // 1023
s = bi.toString(8);                   // 1777

// Parse and format to decimal
bi = new BigInteger("1023");          // 1023
s = bi.toString();                      // 1023

// Parse and format to hexadecimal
bi = new BigInteger("3ff", 16);       // 1023
s = bi.toString(16);                  // 3ff

// Parse and format to arbitrary radix <= Character.MAX_RADIX
int radix = 32;
bi = new BigInteger("vv", radix);     // 1023
s = bi.toString(radix);               // vv

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.