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 &lt;= 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;
}
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:

http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

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