In this post about Java I am going to teach you how to convert text to binary, and binary to text. That is, make a type of text – binary translator in Java to be able to convert between both formats.
Another way to call this is to say “Binary to English translator” for example.
For example, convert 1110000 1100001 1110010 1111010 1101001 1100010 1111001 1110100 1100101 101110 1101101 1100101 to “parzibyte.me” and vice versa.
All this programmed in Java with manual methods and custom logic.
Convert text to binary: algorithm
The algorithm is simple. We have the English text as a string. We extract each letter, and for each one we obtain its ASCII value.
After having its ASCII value as an integer, we convert it to binary or base 2. And once we have it as binary, we store it in a string that will be the result. In this string we add the binary values separating them by a space.
From binary to text
The procedure is almost the inverse of the previous one, but with the difference that now we are going to use split
. We have the binary string (each binary number represents a character) separated by spaces.
We obtain each binary number separately with split.
What split does is separate a string and convert it to an array. Then we go through the array, convert the binary to an integer (which will have the ASCII representation) and get the character it represents.
Conversion functions
For number conversion, the methods that convert a binary number to decimal, and a decimal number to binary, are:
public static int binaryToDecimal(String binary) {
int decimal = 0;
int position = 0;
for (int x = binary.length() - 1; x >= 0; x--) {
// Saber si es 1 o 0; primero asumimos que es 1 y abajo comprobamos
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;
}
Convert text to binary
Let’s see the first case to convert the text to binary. It is a function that receives the string, goes through it, converts each letter and adds it to the result:
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;
}
What the function returns is also a string with the binary text. With this we can translate from string to binary, that is, to zeros and ones.
Binary to text
For the inverse process we also have another function:
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;
}
What I mentioned about split is on line 3. We convert it to an array, we go through each binary value, and we convert it to decimal to obtain its ASCII representation.
Testing the Java translator
To test all this you can use strings written by you, or with text provided by the user, here I am only showing you a simple example, but in the end it can be as you prefer.
Below I will leave the complete code, here I just show you how to use the binary translator:
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);
}
Putting it all together
The complete code looks as follows:
/*
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
____________________________________
/ 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;
}
}
Remember that you can adapt it to your needs or only copy some functions.
If you need more about Java, click here.