Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static String fillWithZeros(String string) {
- if (string.length() == 8)
- return string;
- else
- return fillWithZeros("0"+string);
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.println("Please type 1 for string->binaryString or 2 for binaryString->string");
- int type;
- do {
- type = in.nextInt();
- } while (!(type == 1 || type == 2));
- String chain;
- if (type == 1) {
- System.out.println("Please type in your string to convert");
- in.nextLine();
- chain = in.nextLine();
- String output = "";
- for (char x : chain.toCharArray()) {
- String temp = Integer.toBinaryString((int) x);
- temp = fillWithZeros(temp);
- output += temp;
- }
- System.out.println("Converted string->binaryString: "+output);
- }
- else {
- System.out.println("Please type in your binaryString to convert");
- in.nextLine();
- chain = in.nextLine();
- String[] string = chain.split("");
- String current = "";
- String output = "";
- for (int i = 0; i < string.length; i++) {
- if (i%8 == 0 && i != 0) {
- int a = Integer.parseInt(current, 2);
- output += (char) a;
- current = "";
- }
- current += string[i];
- }
- int a = Integer.parseInt(current, 2);
- output += (char) a;
- System.out.println(output);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement