Reputation: 531
There is a binary String :
String binaryString = "101101010111001011111000";
This string is of length 24, so it can be converted into 3 bytes.
The number that byte can contain is -128 to 127 but in the raw format it contains 8 bits.
Say : binary1
is 10110101
and binary2
is 01110010
and binary3
is 11111000
I want to convert this binaryString into raw bytes but when I am trying
Byte.parseByte(binary1,2);
But this method convert using int and the limit of byte ranges applies.
I want to write this binaryString to the file in form of byte.
What can be the solution to have the raw byte containing 8 bits and nothing treated like number or int ?
Upvotes: 2
Views: 2147
Reputation: 36339
The following should work:
byte theByte = (byte) Integer.parseInt("10101010", 2);
The parsed string must not be longer than 8, otherwise only the right-most 8 bits will be in the variable theByte
.
Please make sure to check your output with appropriate tools (no text editors!).
Upvotes: 2