我想知道是否可以输入二进制数并将它们翻译成文本.例如,我输入"01101000 01100101 01101100 01101100 01101111",然后将其转换为"hello".
只是一些逻辑修正:
这里有三个步骤
幸运的是,parseInt需要radix为基本参数.所以,一旦你把字符串剁成(大概)一个长度为8的字符串数组,或者访问必要的子字符串,你需要做的就是(char)Integer.parseInt(s, 2)连接.
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;
}
Run Code Online (Sandbox Code Playgroud)