Parsing and Formatting a Big Integer into Binary, Octal, and Hexadecimal - Java Language Basics

Java examples for Language Basics:BigInteger

Description

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

Demo Code

import java.math.BigInteger;

public class Main {
  public void m() throws Exception {
    BigInteger bi = new BigInteger("1023");

    // Parse and format to binary
    bi = new BigInteger("111111111111", 2);
    String s = bi.toString(2); /*from   w  w w . ja  v a 2s. c om*/

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

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

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

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

Related Tutorials