[英]Reading bit string from binary file in java
I have a string of bits with length 128 and I want to convert it to an byte array, then write it to a binary file and later read from the binary file and convert byte array to bit string. 我有一个长度为128的位字符串,我想将其转换为字节数组,然后将其写入二进制文件,然后从二进制文件中读取并将字节数组转换为位字符串。 This is my code ( I use an input of length 16 for simplicity):
这是我的代码(为简单起见,我使用长度为16的输入):
String stest = "0001010010000010";
//convert to byte array
short a = Short.parseShort(stest, 2);
ByteBuffer bytes = ByteBuffer.allocate(2).putShort(a);
byte[] wbytes = bytes.array();
System.out.println("Byte length: "+ wbytes.length);
System.out.println("Writing to binary file");
try {
FileOutputStream fos = new FileOutputStream("test.ai");
fos.write(wbytes);
fos.flush();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Reading from binary file");
File inputFile = new File("test.ai");
byte[] rdata = new byte[(int) inputFile.length()];
//byte[] rdata = new byte[2];
FileInputStream fis;
String readstr = "";
try {
fis = new FileInputStream(inputFile);
fis.read(rdata, 0, rdata.length);
fis.close();
for(int i=0; i<rdata.length; i++){
Byte cb = new Byte(rdata[i]);
readstr += Integer.toBinaryString(cb.intValue());
}
System.out.println("Read data from file: " + readstr);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However the string that I read from the file is not equal to the original string. 但是,我从文件中读取的字符串不等于原始字符串。 This is the output:
这是输出:
String: 0001010010000010
Byte length: 2
Writing to binary file
Reading from binary file
Read data from file: 1010011111111111111111111111110000010
I would start to choose data type I use for both scenario. 我将开始选择在两种情况下都使用的数据类型。 Lets think about, you choose byte.
让我们考虑一下,您选择字节。 So, writing it is pretty easy.
因此,编写它非常容易。
byte data[] = ... //your data
FileOutputStream fo = new FileOutputStream("test.ai");
fo.write(data);
fo.close();
now, lets read stringized data from the file. 现在,让我们从文件中读取字符串化数据。 As you know, 1 byte is 8 bit.
如您所知,1字节是8位。 So, you need to just read 8 chars from the file, and convert it to a byte.
因此,您只需要从文件中读取8个字符,然后将其转换为字节即可。 So,
所以,
FileInputStream fi = new FileInputStream("test2.ai"); // I assume this is different file
StringBuffer buf = new StringBuffer();
int b = fi.read();
int counter = 0;
ByteArrayOutputStream dataBuf = new ByteArrayOutputStream();
while (b != -1){
buf.append((char)b);
b = fi.read();
counter++;
if (counter%8 == 0){
int i = Integer.parseInt(buf.toString(),2);
byte b = (byte)(i & 0xff);
dataBuf.write(b);
buf.clear(0,buf.length());
}
}
byte data[] = dataBuf.toByteArray();
I think the problem in your code is converting string to byte. 我认为您代码中的问题是将字符串转换为字节。 Your starting point is already wrong.
您的出发点已经错了。 You are converting your data to short that keeps only 2 byte.
您正在将数据转换为仅保留2个字节的short数据。 But, you say your file can keep 128 bit, that is 16 byte.
但是,您说您的文件可以保留128位,即16字节。 So, you should not try to convert whole file to a data type, like short, integer or long.
因此,您不应尝试将整个文件转换为数据类型,例如short,integer或long。 you have to convert every 8 bit to byte.
您必须将每8位转换为字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.