I wanted to convert an integer to binary string. I opted to use the native function that java allows Integer.toBinaryString(n)
. But unfortunately, this trims the leading zero from my output. Lets say for example, I give the input as 18
, it gives me output of 10010
but the actual output is supposed to be 010010
. Is there a better/short-way to convert int to string than writing a user defined function? Or am I doing something wrong here?
int n = scan.nextInt();
System.out.println(Integer.toBinaryString(n));