二进制到Java文本 [英] Binary to text in Java
本文介绍了二进制到Java文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static String toText(String info)throws UnsupportedEncodingException {
byte [] encoded = info.getBytes();
String text = new String(encoded,UTF-8);
System.out.println(print:+ text);
返回文本;
}
任何更正或建议将不胜感激。
谢谢!
解决方案
您可以使用 Integer.parseInt
,其基数为2(二进制)将二进制字符串转换为整数:
然后,如果您要将相应的字符作为字符串:
String str = new Character((char)charCode).toString();
I have a String with binary data in it (1110100) I want to get the text out so I can print it (1110100 would print "t"). I tried this, it is similar to what I used to transform my text to binary but it's not working at all:
public static String toText(String info)throws UnsupportedEncodingException{
byte[] encoded = info.getBytes();
String text = new String(encoded, "UTF-8");
System.out.println("print: "+text);
return text;
}
Any corrections or suggestions would be much appreciated.
Thanks!
解决方案
You can use Integer.parseInt
with a radix of 2 (binary) to convert the binary string to an integer:
int charCode = Integer.parseInt(info, 2);
Then if you want the corresponding character as a string:
String str = new Character((char)charCode).toString();
这篇关于二进制到Java文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文