6

I wrote this code for converting binary to text .

public static void main(String args[]) throws IOException{

      BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter a binary value:");
      String h = b.readLine();
      int k = Integer.parseInt(h,2);  
      String out = new Character((char)k).toString();
      System.out.println("string: " + out);
      } 
}

and look at the output !

Enter a binary value:
0011000100110000
string: ?

what's the problem?

4
  • What do you want the output to be? Commented Apr 23, 2012 at 15:05
  • the out put should be string , like 0011010100110101 = 55 or 011000010110000101100001 = aaa Commented Apr 23, 2012 at 15:07
  • You want it to get parsed as ASCII....? Or what? Commented Apr 23, 2012 at 15:17
  • I want it to convert to text(ascii) Commented Apr 23, 2012 at 15:19

2 Answers 2

10

instead of

String out = new Character((char)k).toString();

do

String out = String.valueOf(k);

EDIT:

String input = "011000010110000101100001";
String output = "";
for(int i = 0; i <= input.length() - 8; i+=8)
{
    int k = Integer.parseInt(input.substring(i, i+8), 2);
    output += (char) k;
}   
Sign up to request clarification or add additional context in comments.

1 Comment

that's not what I want it converts hexa decimal to string I think
0

Even Simpler:

String out=""+k;

3 Comments

it converts hexa decimal to string ! not binary to string
you want it to convert to a char first and then to string? please be more clear
I want to convert to text (ascii)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.