Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 21, 2020 19:03
Show Gist options
  • Save parzibyte/0ab101ad23a68addb31c1379706c2631 to your computer and use it in GitHub Desktop.
Save parzibyte/0ab101ad23a68addb31c1379706c2631 to your computer and use it in GitHub Desktop.
/*
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
____________________________________
/ If you need help, contact me: \
\ https://parzibyte.me /
------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String originalText = "parzibyte.me";
System.out.println("Original text: " + originalText);
String translatedText = textToBinary(originalText);
System.out.println("In binary, it is: " + translatedText);
System.out.println("-----------------");
String binaryText = "1110000 1100001 1110010 1111010 1101001 1100010 1111001 1110100 1100101 101110 1101101 1100101";
System.out.println("Binary: " + binaryText);
String translatedBinary = binaryToText(binaryText);
System.out.println("In english, it is: " + translatedBinary);
String userText = "";
Scanner sc = new Scanner(System.in);
System.out.println("Type some text and I will convert it to binary:");
userText = sc.nextLine();
translatedText = textToBinary(userText);
System.out.println(translatedText);
System.out.println("Typee some text in binary and I will translate it to english: ");
userText = sc.nextLine();
translatedBinary = binaryToText(userText);
System.out.println(translatedBinary);
}
public static String binaryToText(String binaryText) {
String[] binaryNumbers = binaryText.split(" ");
String text = "";
for (String currentBinary : binaryNumbers) {
int decimal = binaryToDecimal(currentBinary);
char letra = (char) decimal;
text += letra;
}
return text;
}
public static String textToBinary(String originalText) {
String binaryText = "";
for (int i = 0; i < originalText.length(); i++) {
char currentChar = originalText.charAt(i);
int ascii = (int) currentChar;
String binary = decimalToBinary(ascii);
binaryText += binary + " ";
}
return binaryText;
}
public static int binaryToDecimal(String binary) {
int decimal = 0;
int position = 0;
for (int x = binary.length() - 1; x >= 0; x--) {
short digit = 1;
if (binary.charAt(x) == '0') {
digit = 0;
}
double multiplier = Math.pow(2, position);
decimal += digit * multiplier;
position++;
}
return decimal;
}
public static String decimalToBinary(int decimal) {
if (decimal <= 0) {
return "0";
}
String binary = "";
while (decimal > 0) {
short remainder = (short) (decimal % 2);
decimal = decimal / 2;
binary = String.valueOf(remainder) + binary;
}
return binary;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment