Java/Data Type/Binary

Материал из Java эксперт
Перейти к: навигация, поиск

Convert decimal integer to binary number example

 
public class Main {
  public static void main(String[] args) {
    int i = 78;
    String strBinaryNumber = Integer.toBinaryString(i);
    System.out.println(strBinaryNumber);
  }
}
//1001110





Convert Decimal to Binary

 
 
public class Main {
  public static void main(String[] args) {
    int integer = 127;
    String binary = Integer.toBinaryString(integer);
    System.out.println("Binary value of " + integer + " is " + binary + ".");
    int original = Integer.parseInt(binary, 2);
    System.out.println("Integer value of binary "" + binary + "" is " + original + ".");
  }
}





Convert from decimal to binary

 
public class Main {
  public static void main(String[] args) throws Exception {
    int i = 42;
    String bin = Integer.toBinaryString(i);
    System.out.println(bin);
  }
}
//101010





Parsing and Formatting a Number into Binary

 
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);
  }
}