2

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]);

1 Answer 1

2

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.

public static void main(String[] args) throws Exception {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a message: ");
    String message = input.nextLine();

    byte[] messageBytes = message.getBytes();
    // Using OR operation (use 1 - 127)
    for (int i = 0; i < messageBytes.length; i++) {
        messageBytes[i] |= 1;
    }

    System.out.println("Before: " + message);
    System.out.println("After : " + new String(messageBytes));
}

Results:

Enter a message: John has a dog
Before: John has a dog
After : Koio!ias!a!eog

Update to include binary output

public static void main(String[] args) throws Exception {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a message: ");
    String message = input.nextLine();
    byte[] messageBytes = message.getBytes();

    System.out.println("Before: " + message);
    // Display message in binary
    for (int i = 0; i < messageBytes.length; i++) {
        System.out.print(Integer.toBinaryString(messageBytes[i]) + " ");
    }
    System.out.println();

    // OR each byte by 1 as an example of bit manipulation
    for (int i = 0; i < messageBytes.length; i++) {
        messageBytes[i] |= 1;
    }

    System.out.println("After : " + new String(messageBytes));
    // Display message in binary
    for (int i = 0; i < messageBytes.length; i++) {
        System.out.print(Integer.toBinaryString(messageBytes[i]) + " ");
    }
    System.out.println();
}

Results:

Enter a message: John has a dog
Before: John has a dog
1001010 1101111 1101000 1101110 100000 1101000 1100001 1110011 100000 1100001 100000 1100100 1101111 1100111 
After : Koio!ias!a!eog
1001011 1101111 1101001 1101111 100001 1101001 1100001 1110011 100001 1100001 100001 1100101 1101111 1100111 
Sign up to request clarification or add additional context in comments.

8 Comments

ArrayList because size of binary values will be unknown, when user inputs some text by Scanner? Am i wrong with it?
Also i want to manipulate bits i want to (not random or automatic by loop), thats why i want it in Array too.
@Chris92 Once you convert your String to a Byte Array you can manipulate the bits in the Byte Array anyway you want: Bit shifting, AND (&) operation, OR (|) operation. You don't have to have the binary value to manipulate bits. I'm merely demonstrating bit manipulation in the for loop.
Ok, but i need binary output of input text - just to see if Hamming code correctly checked some errors. That's why i need to be able to change some bits after converting from String to Binary. Your code is good, but i need it binary, certainly.
@Chris92 See updates for binary output. Keep in mind I'm only demonstrating how to do this, which may not be to your exact use case, but you should be able to easily adjust the code to fit your exact use case.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.