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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10283010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:22:12  来源:igfitidea点击:

java: converting binary to text?

javabinary

提问by Aida E

I wrote this code for converting binary to text .

我写了这段代码来将 binary 转换为 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?

有什么问题?

回答by juergen d

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

回答by EnKrypt

Even Simpler:

更简单:

String out=""+k;