7

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?

3
  • How many bits are in a byte? Answering this question may instruct you how many bytes you need Commented Jun 18, 2011 at 4:31
  • 1
    1 byte is represented in 8bits Commented Jun 18, 2011 at 4:32
  • And you want 224 bits, so you need how many bytes? Commented Jun 18, 2011 at 4:37

4 Answers 4

25

For each byte:

  • cast to int (happens in the next step via automatic widening of byte to int)
  • 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
Sign up to request clarification or add additional context in comments.

3 Comments

This prints seven characters for me on occasions. This alternative answer works for me.
Use this: System.out.println(Integer.toBinaryString(0x100 + (int) (b & 0xFF)).substring(1)); to print 8 characters
@Duncan a bit late, but I fixed the 7-char issue. The key change was to use b & 255 | 256
2

Try Integer.toString(bytevalue, 2)

Okay, where'd toBinaryString come from? Might as well use that.

Comments

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

Comments

-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.

3 Comments

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, how are data zeros read from file different from normal zeroes..he he :) I dont get it.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.