Categorías: Java

Java: binary to text translator

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:

See the gist on github.

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:

See the gist on github.

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:

See the gist on github.

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:

See the gist on github.

Putting it all together

The complete code looks as follows:

See the gist on github.

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

parzibyte

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

Compartir
Publicado por
parzibyte

Entradas recientes

Receipt designer for thermal printers – Free and open source

In the last months I have been working on a ticket designer to print on…

4 months hace

JavaScript: store and read files with the Origin Private File System

In this post you will learn how to use the Origin Private File System with…

7 months hace

JavaScript: download file with fetch

In this post you will learn how to download a file in the background using…

7 months hace

SQLite3 with vanilla JavaScript and OPFS – Hello world

In this post I will show you how to use SQLite3 directly in the web…

9 months hace

Python Thermal Printing: A Comprehensive Guide for Printing on Thermal Printers

In this tutorial, we'll explore how to effortlessly print receipts, invoices, and tickets on a…

11 months hace

Image printing on Thermal printer

When printing receipts on thermal printers (ESC POS) sometimes it is needed to print images…

11 months hace

Esta web usa cookies.