我有一个文件,其中包含一个字符串,后跟包含二进制数字的字节。
Thisisastring. �J
在我的代码中,我尝试忽略字符串并专注于解码由空格分隔的字节。当我运行代码时,结果似乎是正确的,除了第一个二进制数很多。
StringBuffer buffer = new StringBuffer();
File file = new File(arg);
FileInputStream in = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(in, "UTF8");
Reader inn = new BufferedReader(isr);
int ch;
while ((ch = inn.read()) > -1){
buffer.append((char)ch);
}
inn.close();
String content = buffer.toString();
String temp = new String();
for(int i=0; i<content.length(); i++){
temp += content.charAt(i);
if(content.charAt(i) == ' '){
while(i != content.length()-1){
i++;
byte b = (byte) content.charAt(i);
String x = Integer.toString(b & 0xFF, 2);
System.out.println(x);
}
}
}
结果:
11111101 <- Why is only this one incorrect?
11000
1001010
1011
预期结果:
10010101
00011000
01001010
1011