二进制到Java文本 [英] Binary to text in Java

查看:111
本文介绍了二进制到Java文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制数据的字符串(1110100)我想得到文本,所以我可以打印(1110100将打印t)。我试过这个,它类似于我以前将文本转换为二进制文件,但它根本不起作用:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆