我是 Java 新手。我已使用此代码将二进制字符串更改为文本字符串。
StringBuilder contentBuilder = new StringBuilder();
StringBuffer sb = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new FileReader("filename.txt"));
String jitu;
while ((jitu = in.readLine()) != null) {
contentBuilder.append(jitu);
}
in.close();
} catch (IOException e) {
}
String binput = contentBuilder.toString();
for (int i = 0; i < binput.length()/8; i++)
{
int a = Integer.parseInt(binput.substring(8*i,(i+1)*8),2);
sb.append((char)(a));
}
然后我想通过以下代码将此文本反转为二进制。
byte[] bytes = binput.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes){
int val = b;
for (int i = 0; i < 8; i++){
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}}
但问题是何时我将从 binary.append() 获取输出。这与输入二进制字符串不同。我希望输出与输入相同。提前致谢。