Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Byte to binary String

807605Aug 3 2007 — edited Aug 3 2007
Hi everybody

An easy question. I have a byte[] variable and I want to build a String with the binary representation of this variable. I have created the following code:
byte[] data;
String binary = "";
....
for (int i=0; i<data.length;i++)
{
          	valueLong = data[i] & 0xFF;
            	binary = binary + Long.toBinaryString(valueLong);
}
The problem with this method is that for example if the byte value is 0, the String value of this byte will be "0" and not "00000000". Anybody knows how to convert a byte to an 8-bit binary String?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 31 2007
Added on Aug 3 2007
10 comments
10,345 views