Search
 
SCRIPT & CODE EXAMPLE
 

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



    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: How to get the nth Fibonacci number code in Java using recursion with memoization 
Java :: hdfs java read all files in directory 
Java :: long to double in java 
Java :: java android build secret keys 
Java :: startactivity not working in android adapter 
Java :: bukkit console filter 
Java :: get free player inventory slots spigot 
Java :: how to covert array into a char 
Java :: default java keystore 
Java :: 2d array length in java 
Java :: stack initialization for string java 
Java :: how to create a subclass in java 
Java :: convert jwt claim to string java 
Java :: https://www.baeldung.com/hibernate-inheritance 
Java :: check if LinkedList is empyth java 
Java :: maths.abs function in java 
Java :: string a int java 
Java :: hashmap put 
Java :: java list newline 
Java :: java use space in enums 
Java :: Java comment vérifier une égalité de String 
Java :: Java Find the greatest common divisor of two positive integers. The integers can be large, so you need to find a clever solution. 
Java :: how to sort the arraylist without changing the original arraylist 
Java :: Java 8 merge 2 collections. 
Java :: arrondi java 
Java :: android bottomnav fab 
Java :: lambda function java 
Java :: change element in array java 
Java :: look and feel java 
Java :: transformer une chaine de caractère en nombre java 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =