转换二进制字符串以ASCII文本? [英] Convert binary string to ascii text?

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

问题描述

我不知道是否有可能在二进制数进入并将它们转换回成文字。比如我输入01101000 01100101 01101100 01101100 01101111,它会是隐蔽进字你好。

I was wondering if it is possible to enter in binary numbers and have them translated back into text. For example I would enter "01101000 01100101 01101100 01101100 01101111" and it would covert it into the word "hello".

推荐答案

只是一些合乎逻辑的修正:

Just some logical corrections:

有三个步骤此处


  1. 打开二进制集合到一个整数

  2. 然后整成一个字符

  3. 然后便连接字符串中,你正在构建

幸运的是 parseInt函数花费底部的基数参数。所以,一旦你要么砍该字符串成(presumably)长度为8的字符串数组,的的访问所需的子串,所有你需要做的就是(字符)的Integer.parseInt(S,2)并连接。

Luckily parseInt takes a radix argument for the base. So, once you either chop the string up into (presumably) an array of strings of length 8, or access the necessary substring, all you need to do is (char)Integer.parseInt(s, 2) and concatenate.

String s2 = "";   
char nextChar;

for(int i = 0; i <= s.length()-8; i += 9) //this is a little tricky.  we want [0, 7], [9, 16], etc (increment index by 9 if bytes are space-delimited)
{
     nextChar = (char)Integer.parseInt(s.substring(i, i+8), 2);
     s2 += nextChar;
}

这篇关于转换二进制字符串以ASCII文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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