3

Possible Duplicate:
Binary to text in Java

I'm writing a program that can convert multiple things, but I need help with the converting of binary. I have my code for text-to-binary working, but I'm not sure about binary-to-text. Here is my code for the button which triggers the conversion:

String code = jTextArea5.getText();
if (code == null) {
    System.out.println(jTextArea1.getText( ));
    String writing = jTextArea1.getText();

    byte[] bytes = writing.getBytes();
    StringBuilder binary = new StringBuilder();

    for (byte b : bytes) {
        int val = b;

        for (int i = 0; i < 8; i++){
            binary.append((val & 128) == 0 ? 0 : 1);
            val <<= 1;
        }
        binary.append(' ');
    }

    jTextArea5.setText("" + binary);
}
else
{
   System.out.println(jTextArea1.getText( ));
    String binary = jTextArea1.getText();

    int ascii = Integer.parseInt(binary, 2);
    char character = (char)ascii;

    jTextArea5.setText("" + character); 
}

If anyone knows how I can fix this code to work, that'd be great. Thanks!

NOTE - This bit below works on it's own, just not in conjunction with any efforts to allow converting binary in jTextArea5 to text in jTextArea1.

    System.out.println(jTextArea1.getText( ));
    String writing = jTextArea1.getText();

    byte[] bytes = writing.getBytes();
    StringBuilder binary = new StringBuilder();

    for (byte b : bytes) {
        int val = b;

        for (int i = 0; i < 8; i++){
            binary.append((val & 128) == 0 ? 0 : 1);
            val <<= 1;
        }
        binary.append(' ');
    }

    jTextArea5.setText("" + binary);
Community
  • 1
  • 1
Razor Shadow
  • 65
  • 1
  • 6

2 Answers2

2

Convert the binary into an integer:

String binary = "010101";    
int ascii = Integer.parseInt(binary, 2);

Then turn the integer into ascii:

char character = (char)ascii;
case1352
  • 1,126
  • 1
  • 13
  • 22
  • I've only been learning Java for a week sorry, so my understanding is limited. Where exactly do these go in the code, and how do they work? – Razor Shadow Oct 19 '12 at 01:20
  • Yes, I'll explain the layout: There are two text areas, one below the other. Next to them are a button named Convert and a button named Wipe. Wipe simply wipes the contents of both boxes. Convert used to convert text in the first box to binary in the second. I'm now trying to allow for input of binary in the second to get text in the first. – Razor Shadow Oct 19 '12 at 01:29
  • Okay, all is good now except for this line (it says unexpected type, required value but found class): jTextArea5.setText("" + char); – Razor Shadow Oct 19 '12 at 01:39
  • Wait, I saw the extension of char to character in your revision history, and tried it. Everything looked okay, no visible errors, but then when I ran the program it I received a whole bunch of errors... I'm going to put my entire code up, not just the text-to-binary bit, in case it changes anything (which I have a feeling it does). – Razor Shadow Oct 19 '12 at 02:01
1

Swap the 5 and the 1 around so it grabs the binary out of the correct textbox.

And use StringTokenizer to process each block of 8

Dont forget to import StringTokenizer

    else
    {
       System.out.println(jTextArea5.getText( ));
        String binary = jTextArea5.getText();
        StringTokenizer st = new StringTokenizer(binary," ");
         while(st.hasMoreTokens()){
             int ascii = Integer.parseInt(st.nextToken(), 2);
             char character = (char)ascii;
             jTextArea1.setText(jTextArea1.getText() + "" + character); 

         }
    }

this bit checks whats in jTextArea5.getText()

if (code == null) {

change it to

if (code.equals("")) {

and make sure you clear out whats in the text boxes before you start either conversion

case1352
  • 1,126
  • 1
  • 13
  • 22
  • Ah, it works! But converting text to binary has stopped working for some reason. As shown in the question, that piece of code works on its own, but inside this if/else piece it just stops working for unknown reasons... Can you figure that out? – Razor Shadow Oct 19 '12 at 02:27
  • Yes, it's working perfectly now! Thanks so much :) Out of curiosity, what's the difference between the 'code == null' version and the 'code.equals(""))' version? – Razor Shadow Oct 19 '12 at 02:37