Java Data Type How to - Parse and Format Binary Number to Decimal, back and forth








Question

We would like to know how to parse and Format Binary Number to Decimal, back and forth.

Answer

/*from ww  w  .j  a va 2 s. c  om*/
public class Main {
  public static void main(String[] argv) throws Exception {
    int i = 1023;
 
    i = Integer.parseInt("1111111111", 2);
    String s = Integer.toString(i, 2); 
    System.out.println(s);
  }
}

The code above generates the following result.