1

I have read several posts on this topic, but none of the codes provided (sometimes fragments) helped me to resolve my simple problem of converting a binary string like this one:

01100111 01101111 01101111 01100100 00100000 01001101 01101111 01110010 01101110 01101001 01101110 01100111 00100000

Into a text string Good Morning

I am probably unable to put together the code fragments that I found in the links that are available in stackoverflow. Would someone be so kind and give me the full code? Assume i have the input string (text) coming from a JTextarea which I am accessing through "gettext()" e.g. text = tTextarea.gettext();

so it is the text would be -

01100111 01101111 01101111 01100100 00100000 01001101 01101111 01110010 01101110 01101001 01101110 01100111 00100000)

that needs to be processed to the answer. Assume I will put the output string (=answer) into the same Textarea with "settext(). ttextarea.settext(answer)

thanks in advance.

cowls
  • 24,013
  • 8
  • 48
  • 78
  • 2
    "so kind and give me the full code" - that's not how this site works. Better find another "do my homework for me" site. – duffymo Jan 24 '13 at 10:17
  • Close to being a good question... Instead of asking us to do it for you, if you show us what you've tried and where you are stuck then I'm sure you will get some help. – cowls Jan 24 '13 at 10:21
  • Did do my homework. However, without success. The way I AM LEARNING is through answers that work. So it is silly, not to provide help. Maybe I COULD HELP once on another topic, where I have gone through many working codes and learned the tricks.. – sabina compassi Jan 26 '13 at 20:06
  • @sabinacompassi: if you did do it without success, then show us what you did and ask how to fix that. If we see *what* you wrote, we can help you *a lot better* because we will know where you are stuck. As the question stands, it's simply a "do it for me"-request. – Joachim Sauer Jan 28 '13 at 08:17
  • [Duplicated] Here is the answer: http://stackoverflow.com/questions/4211705/binary-to-text-in-java In summary: First of all, use `Integer.parseInt(String s, int radix)` to convert it to an Integer with radix 2 (binary). Then transform this Integers to String. – arutaku Jan 24 '13 at 10:20

1 Answers1

1

Here is the code that solves your request:

public static String int2str( String s ) { 
    String[] ss = s.split( " " );
    StringBuilder sb = new StringBuilder();
    for ( int i = 0; i < ss.length; i++ ) { 
        sb.append( (char)Integer.parseInt( ss[i], 2 ) );                                                                                                                                                        
    }   
    return sb.toString();
}   

For your input:

01100111 01101111 01101111 01100100 00100000 01001101 01101111 01110010 01101110 01101001 01101110 01100111 00100000

The output is:

good Morning
arutaku
  • 5,937
  • 1
  • 24
  • 38
  • you should edit your first answer and put this in there! Furthermore, it's *bad form* to provide full code, the OP has no incentive to learn... – Nim Jan 24 '13 at 10:56
  • Ok, I'm sorry. I will take into account your words next time. – arutaku Jan 24 '13 at 11:02
  • Thank you for breaking the "rules" of this platform for me. Now I have all possible conversions. Putting all the codes together and one next to each other will help me to understand and will put me on a higher level. – sabina compassi Jan 28 '13 at 07:55