Aida E
Aida E

Reputation: 1100

java: converting binary to text?

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?

Upvotes: 6

Views: 15506

Answers (2)

juergen d
juergen d

Reputation: 204894

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;
}   

Upvotes: 10

EnKrypt
EnKrypt

Reputation: 777

Even Simpler:

String out=""+k;

Upvotes: 0

Related Questions