将二进制数据转换为java中的字符 [英] Converting binary data to characters in java

查看:256
本文介绍了将二进制数据转换为java中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其内容只有0和1的

I have a file in which its content is only 0's and 1's

我想写一个java程序,将每个8位的0和1转换为char
或换句话说,将文件内容从二进制转换为chars。

I want to write a java program that convert each 8 bit of 0's and 1's to char or in other word, to convert the file content from binary to chars.

为此目的可以使用哪个函数?

Which function can I use for that purpose?

推荐答案

这也是工作

    String s = "0110100001100101011011000110110001101111";
    String str = "";

    for (int i = 0; i < s.length()/8; i++) {

        int a = Integer.parseInt(s.substring(8*i,(i+1)*8),2);
        str += (char)(a);
    }

    System.out.println(str);

这篇关于将二进制数据转换为java中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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