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?
-
How many bits are in a byte? Answering this question may instruct you how many bytes you needAtreys– Atreys2011-06-18 04:31:10 +00:00Commented Jun 18, 2011 at 4:31
-
11 byte is represented in 8bitsPramod– Pramod2011-06-18 04:32:34 +00:00Commented Jun 18, 2011 at 4:32
-
And you want 224 bits, so you need how many bytes?Atreys– Atreys2011-06-18 04:37:36 +00:00Commented Jun 18, 2011 at 4:37
4 Answers
For each byte:
- cast to
int(happens in the next step via automatic widening ofbytetoint) - 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
3 Comments
System.out.println(Integer.toBinaryString(0x100 + (int) (b & 0xFF)).substring(1)); to print 8 charactersb & 255 | 256Try Integer.toString(bytevalue, 2)
Okay, where'd toBinaryString come from? Might as well use that.
Comments
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
Comments
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.