1

I need to convert a whole in binary, my problem is that if I have 011001, I find myself at the end of the conversion 111. How can I leave the zeros? and maintain so the conversion as it should be. Thank you.

 int value = 8388607;
 String bin = Convert.ToString(value, 2);

SOLUTION:

String bin = Convert.ToString(value, 2).PadLeft(X,'0');

where x is the number of bits that make up the string

3
  • is not the same question, he asks for conversion, I wonder how leave 0 in the conversion. Commented Dec 1, 2016 at 14:32
  • I've reopened the question: I agree with Mr. Developer, it's not just a conversion. Commented Dec 1, 2016 at 14:40
  • Does this answer your question? Convert an integer to a binary string with leading zeros Commented Jun 5, 2021 at 17:47

1 Answer 1

3

PadLeft() should do it

     int value = 8388607;
     String bin = Convert.ToString(value, 2).PadLeft(32, '0');

Change 32 to how many bits you would like the number to show

Sign up to request clarification or add additional context in comments.

2 Comments

How is this different from what is already written as the solution?
It's not, OP edited in the solution while I added my answer. Got posted together

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.