Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

how to convert a string of characters to a stream of binary characters binary

import java.util.Arrays;
import java.util.Scanner;

public class Base64 {

    int convertToAscii(char character) {
        int ascii = (int) character;
        return ascii;
    }

    String convertToBinary(int ascii) {
        String binaryString = ""; // Empty Binary String

        while (ascii != 0) {
            int remainder = ascii % 2;
            binaryString = remainder + binaryString; // concatenation 
            ascii = ascii / 2;
        }

        return binaryString;
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int val = 0;
        String zero = "0";
        String bin = "";
        Base64 base = new Base64();

        System.out.print("Enter a String: ");
        String inputString = scanner.next();
        String[] asciiToBin = new String[inputString.length()];

        for (int i = 0; i <= inputString.length() - 1; i++) {

            char character = inputString.charAt(i);
            int ascii = base.convertToAscii(character);

            System.out.println("The ASCII value is " + ascii);

            asciiToBin[i] = zero + base.convertToBinary(ascii);
            bin = Arrays.toString(asciiToBin);

        }
        System.out.println("The Binary Value is " +bin);



    }
}
 
PREVIOUS NEXT
Tagged: #convert #string #characters #stream #binary #characters #binary
ADD COMMENT
Topic
Name
1+7 =