Pramod
Pramod

Reputation: 1461

how to get the binary values of the bytes stored in byte array

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?

Upvotes: 7

Views: 31269

Answers (4)

Josef Panerio
Josef Panerio

Reputation: 61

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

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

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

Upvotes: 25

Suraj Chandran
Suraj Chandran

Reputation: 24791

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.

Upvotes: -2

Charlie Martin
Charlie Martin

Reputation: 112356

Try Integer.toString(bytevalue, 2)

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

Upvotes: 2

Related Questions