I want to input some text by Scanner, like "John has a dog".
Then i want to convert it (by ASCII table for example) to binary values, so it'll be: 01001010011011110110100001101110 011010000110000101110011 01100001 011001000110111101100111
.
And I want to put those binary values in Array or ArrayList (to be able to change some bits).
Then I want to reconvert it, so show it again as a String but with changed some bits, so that sentence should be changed from original "John has a dog".
I have such code, which doesn't seem to work, because char cannot be converted to String.
Scanner skaner = new Scanner (System.in);
String s = skaner.nextLine();
byte[] bytes = s.getBytes();
StringBuilder binary = new StringBuilder();
// for(int i=0;i<length;i++){
// hemisphere [i]=new StringBuilder();
// }
ArrayList<Byte> bajt = new ArrayList<>();
// byte[] bin = new byte[seqLenght];
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
bajt.add(binary.toString().split(' '));
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
//binary.append(' ');
}
System.out.println("tab"+bytes[1]);
I don't understand why you need to have the ArrayList of binary to manipulate the bits. Once you have the byte array you have everything you need to do bit manipulation.
Results:
Update to include binary output
Results: