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

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110
  • is not the same question, he asks for conversion, I wonder how leave 0 in the conversion. – Mr. Developer Dec 01 '16 at 14:32
  • I've reopened the question: I agree with Mr. Developer, it's not just a conversion. – Dmitry Bychenko Dec 01 '16 at 14:40
  • Does this answer your question? [Convert an integer to a binary string with leading zeros](https://stackoverflow.com/questions/23905188/convert-an-integer-to-a-binary-string-with-leading-zeros) – Jason C Jun 05 '21 at 17:47

1 Answers1

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

Nitay
  • 4,193
  • 6
  • 33
  • 42