i am working on a project that gets the data from the file into a byte array and adds "0" to that byte array until the length of the byte array is 224 bits. I was able to add zero's but i am unable to confirm that how many zero's are sufficient. So i want to print the file data in the byte array in binary format. Can anyone help me?
Asked
Active
Viewed 3.0k times
4 Answers
24
For each byte:
- cast to
int
(happens in the next step via automatic widening ofbyte
toint
) - bitwise-AND with mask 255 to zero all but the last 8 bits
- bitwise-OR with 256 to set the 9th bit to one, making all values exactly 9 bits long
- invoke
Integer.toBinaryString()
to produce a 9-bit String - invoke
String#substring(1)
to "delete" the leading "1", leaving exactly 8 binary characters (with leading zeroes, if any, intact)
Which as code is:
byte[] bytes = "\377\0\317\tabc".getBytes();
for (byte b : bytes) {
System.out.println(Integer.toBinaryString(b & 255 | 256).substring(1));
}
Output of above code (always 8-bits wide):
11111111
00000000
11001111
00001001
01100001
01100010
01100011

Bohemian
- 412,405
- 93
- 575
- 722
-
2This prints seven characters for me on occasions. [This alternative answer](http://stackoverflow.com/a/12310078/474189) works for me. – Duncan Jones Jun 14 '13 at 12:38
-
Use this: `System.out.println(Integer.toBinaryString(0x100 + (int) (b & 0xFF)).substring(1));` to print 8 characters – zapstar Aug 30 '13 at 17:22
-
1@Duncan a bit late, but I fixed the 7-char issue. The key change was to use `b & 255 | 256` – Bohemian Jul 08 '14 at 02:11
2
Try Integer.toString(bytevalue, 2)
Okay, where'd toBinaryString
come from? Might as well use that.

Charlie Martin
- 110,348
- 25
- 193
- 263
0
You can work with BigInteger like below example, most especially if you have 256 bit or longer.
Put your array into a string then start from there, see sample below:
String string = "10000010";
BigInteger biStr = new BigInteger(string, 2);
System.out.println("binary: " + biStr.toString(2));
System.out.println("hex: " + biStr.toString(16));
System.out.println("dec: " + biStr.toString(10));
Another example which accepts bytes:
String string = "The girl on the red dress.";
byte[] byteString = string.getBytes(Charset.forName("UTF-8"));
System.out.println("[Input String]: " + string);
System.out.println("[Encoded String UTF-8]: " + byteString);
BigInteger biStr = new BigInteger(byteString);
System.out.println("binary: " + biStr.toString(2)); // binary
System.out.println("hex: " + biStr.toString(16)); // hex or base 16
System.out.println("dec: " + biStr.toString(10)); // this is base 10
Result:
[Input String]: The girl on the red dress.
[Encoded String UTF-8]: [B@70dea4e
binary: 101010001101000011001010010000001100111011010010111001001101100001000000110111101101110001000000111010001101000011001010010000001110010011001010110010000100000011001000111001001100101011100110111001100101110
hex: 546865206769726c206f6e20746865207265642064726573732e
You can also work to convert Binary to Byte format
try {
System.out.println("binary to byte: " + biStr.toString(2).getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {e.printStackTrace();}
Note: For string formatting for your Binary format you can use below sample
String.format("%256s", biStr.toString(2).replace(' ', '0')); // this is for the 256 bit formatting

Josef Panerio
- 41
- 2
-2
First initialize the byte array with 0s:
byte[] b = new byte[224];
Arrays.fill(b, 0);
Now just fill the array with your data. Any left over bytes will be 0.

Suraj Chandran
- 24,433
- 12
- 63
- 94
-
what if the file actually has zeros at the end of its data? You wouldn't be able to tell the difference between initialized zeros and actual zeros – Bohemian Jun 18 '11 at 06:09
-
@bohemian, how are data zeros read from file different from normal zeroes..he he :) I dont get it. – Suraj Chandran Jun 18 '11 at 06:45
-
you said "Any left over bytes will be 0", but that's not necessarily true - if the file contains trailing zeros, with your logic it would appear to have not loaded enough data, but it would have – Bohemian Jun 18 '11 at 23:44