String message= "10";
byte[] bytes = message.getBytes();
for (int n = 0; n < bytes.length; n++) {
byte b = bytes[n];
for (int i = 0; i < 8; i++) {//do something for each bit in my byte
boolean bit = ((b >> (7 - i) & 1) == 1);
}
}
My problem here is that it takes 1
and 0
as their ASCII values, 49
and 48
, instead of 1
and 0
as binary(00000001
and 00000000
). How can I make my program treat each character from my string as a binary sequence of 8 bits?
Basicly, I want to treat each bit of my number as a byte. I do that like this byte b = bytes[n];
but the program treats it as the ASCII value.
I could assign the number to an int, but then, I can't assign the bits to a byte.