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.

Text to binary translator in Java

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:

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:

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:

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:

Putting it all together

The complete code looks as follows:

Remember that you can adapt it to your needs or only copy some functions. As always, you can execute and test the code here.

If you need more about Java, click here.


I am available for hiring if you need help! I can help you with your project or homework feel free to contact me.
If you liked the post, show your appreciation by sharing it, or making a donation

Suscribir por correo

Ingresa tu correo y recibirás mis últimas entradas sobre programación, open source, bases de datos y todo lo relacionado con informática

Join 4,207 other subscribers


parzibyte

Freelancer programmer ready to work with you. Web, mobile and desktop applications. PHP, Java, Go, Python, JavaScript, Kotlin and more :) https://parzibyte.me/

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.

%d bloggers like this: