# Efficient text to binary conversion in Java

I wrote a Java FX application that translates binary to and from String and numbers in Java. Whenever the program is translating large amounts of text to binary it takes a very long time, and leaves the application unresponsive. Could a more efficient algorithm be used to speed up the process?

    // Turns binary into text.
private void binaryToText() throws BinaryFormatException
{
if (input.toString().length() % 8 != 0)
throw new BinaryFormatException("Binary must be a multiple of 8");

String input = this.input.toString();
String _byte = "", textResult = "";

for (int i = 1 ; i <= input.length() ; i++) {
_byte += input.charAt(i-1);
if (i % 8 == 0) {
int charCode = Integer.parseInt(_byte, 2);
textResult += (char) charCode;
_byte = "";
}
}

this.output = textResult;
}

// Turns text into binary.
private void textToBinary()
{
String binResult = "";
final String fourBitPrepend = "0000";
final String twoBitPrepend = "00";
final String bitPrepend = "0";
byte[] charCodes = input.toString().getBytes();

for(byte ch : charCodes) {
String binary = Integer.toBinaryString(ch);
switch(binary.length()) {
case 4 : binResult += fourBitPrepend + binary; break;
case 6 : binResult += twoBitPrepend + binary; break;
case 7 : binResult += bitPrepend + binary; break;
default : binResult += binary;
}
}

this.output = binResult;
}

• The algorithmic problem causing a very long time to be taken is not visible in the in the code presented: appending single chars to a String causes a new StringBuilder to be instantiated, initialised with the String's contents, and extended with the char. Then, a new StringBuilder to be instantiated and initialised with the String's contents - in a loop, consuming noticeable time in no time: use StringBuilder & append(). (Repeated input.toString() doesn't help.) – greybeard Jul 10 '16 at 5:08
• (The "String +-issue" aside, your binaryToText() looks even weirder than the textToBinary(): please disclose the type of data member input. If this is an assignment in learning Java, please tell to help avoid spoiling a learning experience.) – greybeard Jul 10 '16 at 5:43

Sure, you may speed up your code execution. But the core problem remains: Inresponsive UI executing a long running task.

If you expect the user wait for completion a long running task you should consider UI supported synchronized mechanisms to adress it.

Asynchronous execution is mentioned here:

The idea is: Execute your code in a separate thread and synchronize results by publishing them through the Java FX thread.

JavaFX Nodes runs on a separate UI Thread and this Thread should never be used other than to update the UI Components.. Always use a separate Thread to do the operations not related to UI. see this PrimeFinder Example.

And here is my version of your binaryToText

private static String binaryToText(String input) {

if (input.length() % 8 != 0) {
throw new IllegalArgumentException("input must be a multiple of 8");
}

StringBuilder result = new StringBuilder();

for (int i = 0; i <input.length(); i+=8) {
int charCode = Integer.parseInt(input.substring(i,i+8), 2);
result.append((char) charCode);
}

return result.toString();
}

• Make it a habit to provide a size to constructors if you got a good guess (final byte[] textBytes = input.getBytes(); final StringBuilder binary = new StringBuilder(textBytes.length*8);. (The whole idea of implementing something without a specification is fallacious - and the binaryToText(String input) ended up doing something dissimilar. Going "bare metal" for textToBinary() would eliminate leading-zero-contortions but for the funny case 4/6/7 from the question.) – greybeard Aug 4 '16 at 6:17
• @greybeard u caught me. i just removed the code for now till i got it with proper specification – Mak Aug 4 '16 at 8:27