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

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

问题描述

我有一个包含二进制数据的字符串 (1110100) 我想取出文本以便我可以打印它(1110100 会打印t").我试过了,它类似于我用来将文本转换为二进制的方法,但它根本不起作用:

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.

谢谢!

推荐答案

您可以使用 Integer.parseInt 以 2(二进制)为基数将二进制字符串转换为一个整数:

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天全站免登陆