In Java, we can use Integer.toBinaryString(int) to convert an Integer to a binary string representative. Integer.java public final class Integer extends Number implements Comparable<Integer>, Constable, ConstantDesc { public static String toBinaryString(int i) { return toUnsignedString0(i, 1); } //… } This article will show you two methods to convert an Integer to a binary string representative. […]

Read more Java – Convert Integer to Binary

In Java, we can use Integer.toBinaryString(int) to convert a byte to a binary string representative. Review the Integer.toBinaryString(int) method signature, it takes an integer as argument and returns a String. Integer.java public final class Integer extends Number implements Comparable<Integer>, Constable, ConstantDesc { public static String toBinaryString(int i) { return toUnsignedString0(i, 1); } //… } If […]

Read more Java – How to convert a byte to a binary string

Review the following Java example to convert a negative integer in binary string back to an integer type. String binary = Integer.toBinaryString(-1); // convert -1 to binary // 11111111 11111111 11111111 11111111 (two’s complement) int number = Integer.parseInt(binary, 2); // convert negative binary back to integer System.out.println(number); // output ?? The result is NumberFormatException! Exception […]

Read more Java – Convert negative binary to Integer

In Python, we can use bin() or format() to convert an integer into a binary string representation. print(bin(1)) # 0b1 print(bin(-1)) # -0b1 print(bin(10)) # 0b1010 print(bin(-10)) # -0b1010 print("{0:b}".format(10)) # 1010 print("{0:#b}".format(10)) # 0b1010 , with 0b prefix print("{0:b}".format(10).zfill(8)) # 00001010 , pad zero, show 8 bits print(format(10, "b")) # 1010 print(format(10, "#b")) # […]

Read more Python – How to convert int to a binary string?