title: How to convert a byte to its binary string representation
body: <p>For example, the bits in a byte <code>B</code> are <code>10000010</code>, how can I assign the bits to the string <code>str</code> literally, that is, <code>str = &quot;10000010&quot;</code>.</p>
  <p><strong>Edit</strong></p>
  <p>I read the byte from a binary file, and stored in the byte array <code>B</code>. I use <code>System.out.println(Integer.toBinaryString(B[i]))</code>.  the problem is</p>
  <p>(a) when the bits begin with (leftmost) 1, the output is not correct because it converts <code>B[i]</code> to a negative int value.</p>
  <p>(b) if the bits begin with <code>0</code>, the output ignore <code>0</code>, for example, assume <code>B[0]</code> has 00000001, the output is <code>1</code> instead of <code>00000001</code></p>