Communities for your favorite technologies. Explore all Collectives
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
when converting int to binary, how to output it to 8 character, currently it only display 1 and short of the 7 zeros
int x = 1; String bin = Integer.toBinaryString(x); System.Out.Println(bin);
example output to 0000 0001
I am not sure if that is what you mean but how about something like
String.format("%8s", Integer.toBinaryString(1)).replace(' ', '0')
will generate 00000001
00000001
Add a comment
You can pad your binary string with zeroes.
String bin = String.format("%8s", Integer.toBinaryString(x).replace(' ', '0');
Convert, check the resulting length and add zeros if < 8.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.