java:将二进制转换为文本?

时间:2012-04-23 15:00:02

标签: java binary

我写了这个代码,用于将二进制转换为文本。

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

并查看输出!

Enter a binary value:
0011000100110000
string: ?

问题是什么?

2 个答案:

答案 0 :(得分:9)

而不是

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

DO

String out = String.valueOf(k);

编辑:

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

答案 1 :(得分:0)

更简单:

String out=""+k;