[英]How to maintain consistency in the binary data written to a file?
I am trying to write a binary string into a file and afterwords read it and present it in a hex representation. 我正在尝试将二进制字符串写入文件,并且后记读取它并将其以十六进制表示形式呈现。 This is my code: 这是我的代码:
import java.io.*;
import java.math.BigInteger;
public class TestByte {
public static void main(String[] argv) throws IOException {
String bin = "101101010110001000010011000000000100010000000001000000000000000100000000000000000000000000000000000010110000000000111111000101010001100000000000000000000000000000001000000000000000000000111110011111000000000010110011";
//write binary file
DataOutputStream out = null;
File fileout = new File("binout.bin");
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileout)));
int indx = 0;
for (int i = 0; i < bin.length()/8; i++){
System.out.println(bin.substring(indx, indx+8));
byte[] bytesout = new BigInteger(bin.substring(indx, indx+8),2).toByteArray();
out.write(bytesout);
indx += 8;
}
out.close();
//read binary file
File filein = new File("binout.bin");
byte[] bytesin = new byte[(int) filein.length()];
FileInputStream inputStream = new FileInputStream(filein);
inputStream.read(bytesin);
inputStream.close();
StringBuilder sb = new StringBuilder();
for (byte b : bytesin) {
sb.append(String.format("%02X ", b));
}
System.out.println(sb.toString());
}}
The program works, however, there are some inconsistencies with the data. 该程序有效,但是与数据存在一些不一致之处。 This is the output: 这是输出:
10110101 01100010 00010011 00000000 01000100 00000001 00000000 00000001 00000000 00000000 00000000 00000000 00001011 00000000 00111111 00010101 00011000 00000000 00000000 00000000 00001000 00000000 00000000 00111110 01111100 00000000 10110011 10110101 01100010 00010011 00000000 01000100 00000001 00000000 00000001 00000000 00000000 00000000 00000000 00001011 00000000 00111111 00010101 00011000 00000000 00000000 00000000 00001000 00000000 00000000 00111110 01111100 00000000 10110011
00 B5 62 13 00 44 01 00 01 00 00 00 00 0B 00 3F 15 18 00 00 00 08 00 00 3E 7C 00 00 B3 00 B5 62 13 00 44 01 00 01 00 00 00 00 0B 00 3F 15 18 00 00 00 08 00 00 3E 7C 00 00 B3
As you can see, I have broken the binary string into pieces of 8 bits so it would be easier to follow the numbers. 如您所见,我将二进制字符串分成了8位,因此跟随数字会更容易。 The inconsistency is the Hex representation. 不一致是十六进制表示。 It seems there is an extra "00" at the beginning of the hex string, which should not be there. 十六进制字符串的开头似乎有一个额外的“ 00”,不应在该位置。 Also there is an extra "00" in the end of the string, the should be only one "00" before the "B3". 字符串的末尾还有一个额外的“ 00”,“ B3”之前应该只有一个“ 00”。
Can anyone shed some light on this problem and/or make the solution more elegant? 谁能阐明这个问题和/或使解决方案更加优雅? Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。
You should use Integer.parseInt like this to avoid the sign / 2s complement problem. 您应该像这样使用Integer.parseInt以避免符号/ 2s补码问题。
This is working for me: 这对我有用:
for (int i = 0; i < bin.length()/8; i++){
System.out.println(bin.substring(indx, indx+8));
int b = Integer.parseInt(bin.substring(indx, indx+8),2);
out.writeByte(b);
indx += 8;
}
See also this: Converting a String representation of bits to a byte 另请参见: 将位的字符串表示形式转换为字节
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.