2

Is there an easy way to print byte[] array ( zeros and ones, basically to every bit convert in ascii '1' or ascii '0') to the console ?

4
  • I'm not exactly sure what you want. Could you give us an example of the expected output? Commented Feb 2, 2011 at 13:24
  • do you need this for debugging purposes only? Commented Feb 2, 2011 at 13:28
  • @Joachim Sauer If I understand it correctly, OP wants something like Integer.toBinaryString() for bytes. Baffles me why there's no such method in the Byte class, to be honest. Commented Feb 2, 2011 at 13:31
  • @biziclop - ah, well I read the question in a different way - that his byte array contains only zeros and ones. @Damir - please clarify Commented Feb 2, 2011 at 13:34

4 Answers 4

8

You can output the individual bytes by converting their numeric value to base 2. Here are two ways to do it. In both, I will use this byte array:

byte[] array = "HälLø123§$%".getBytes();

Walk the array

for(final byte b : array){
    System.out.print(Integer.toString(b & 0xFF /* thx Jason Day */, 2));
}

Output:

10010001100001110100100110110010011001100001110111000110001011001001100110110000101010011110010001001010

Reference:

Use BigInteger

If you want to output the entire array in one piece, use BigInteger:

System.out.println(new BigInteger(array).toString(2));

Output:

100100011000011101001000110110001001100110000111011100000110001001100100011001111000010101001110010010000100101

Reference:

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

2 Comments

Careful, neither of these methods take sign extension into account. For example, Integer.toString((byte) 130, 2) results in -1111110, not 10000010. You need to mask off the sign bit first.
@Jason that's true for the first version (and I adjusted it, thanks) but not for the second.
3

If you want to print the binary representation of each element, you can do something like this:

StringBuilder sb = new StringBuilder();
sb.append("[");
String comma = "";
for (byte b : array) {
    int i = b & 0xFF; // cast to int and mask the sign bit
    sb.append(comma);
    comma = ",";
    sb.append(Integer.toBinaryString(i));
}
sb.append("]");

System.out.println(sb.toString());

Comments

3

You can use commons-codec BinaryCodec:

BinaryCodec.toAsciiString(array)

For better control on the output, you can use toAsciiChars(..) and print the char[].

(If you simply want to output the array values - java.util.Arrays.toString(array));

4 Comments

That would display the bytes in decimal though, wouldn't it?
@biziclop - no, nothing decimal. it will print the contents of the byte array.
Although it wasn't from me, I guess because you failed to answer the question. Which was, how to print the array in binary, not decimal.
@biziclop aha, I added an additional option for that.
2

Note: This outputs individual bytes, not individual bits. But as it may still be useful for someone, I'll let it here.


Here the solution for my understanding of the question:

byte[] data = {0, 1, 0, 1, 1, 0};
StringBuilder sb = new StringBuilder(data.length);
for(byte b : data) {
    sb.append(b);
}
System.out.println(sb);

This should output 010110.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.