java integer to binary string with leading zeros 1

java integer to binary string with leading zeros

int n = 1000;
String.format("%16s", Integer.toBinaryString(n).replace(' ', '0');

Here is what the above code is Doing:
1. Integer.toBinaryString(n) converts the integer to a binary string.
2. .replace(‘ ‘, ‘0’) replaces all spaces with 0s.
3. String.format(“%16s”, …) pads the string with spaces on the left so that it is 16 characters long.

Similar Posts