Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.