I readed a file using Java and use HexDump to output the data. It looks like this: The first and second line: one:31 30 30 31 30 30 30 31 31 30 30 31 30 31 31 31 two: 30 31 31 30 30 31 31 30 31 31 30 30 31 31 30 31 I want to print the data between first "31 30 30 31"and the second "31 30 30 31".My ideal ouput is 31 30 30 31 30 30 30 31 31 30 30 31 30 31 31 31 30 31. But the real output is wrong,I think my code can not find the 31 30 30 31 in the data1.How to figure it out?
I Use jdk 1.7 and the software is idea
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.File;
public class TestDemo{
public static void main(String[] args) {
try {
File file = new File("/0testData/1.bin");
DataInputStream isr = new DataInputStream(newFileInputStream(file));
int bytesPerLine = 16;
int byteCount = 0;
int data;
while ((data = isr.read()) != -1) {
if (byteCount == 0)
System.out.println();
else if (byteCount % bytesPerLine == 0)
System.out.printf("\n",byteCount );
else
System.out.print(" ");
String data1 = String.format("%02X",data & 0xFF);
System.out.printf(data1);
byteCount += 1;
if(data1.contains("31 30 30 31")) {
int i=data1.indexOf("31 30 30 31",12);
System.out.println("find it!");
String strEFG=data1.substring(i,i+53);
System.out.println("str="+strEFG);
}else {
System.out.println("cannot find it");
}
}
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
My ideal ouput is 31 30 30 31 30 30 30 31 31 30 30 31 30 31 31 31 30 31. But the real output is:
31cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 30cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it 31cannot find it
30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 31cannot find it
31cannot find it 31cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 30cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it 31cannot find it
31cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it 31cannot find it 31cannot find it 31cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it
30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 30cannot find it
I feel that your input data is a bit confusing. Nevertheless, this probably answers your question.
It doesn't give quite the same output that you are asking for, but I think you should be able to tweak it to turn on or off the output by using the flag "inPattern". If inPattern is true, print your data read from the file, if false, do not print the data read from the file.
This is probably not the best form of coding as it is entirely static methods - but it does what you ask for.
The problem with your code (I think) is that data1 will be a 2 character string. It is impossible for it to contain a 11 character string ("31 30 30 31"). If you tried reversing the test (i.e. "31 30 30 31".contains(data1)) then it will only be matching a single byte - not the 4 bytes you are intending to match.
There is a small problem with this code in that it does not mark the beginning pattern, but does mark the ending pattern. Hopefully this is not a problem for you. If you need to correctly mark the beginning or not mark the ending, then there will be another level of complexity. Basically you would have to read ahead in the file and write the data out 4 bytes behind the data you have been reading. This could be achieved by printing the value that comes off of the buffer at the line which reads:
rather than printing the value read from the file (i.e. the value in the "data" variable).
Following is an example of the data produced when run against a PNG file of an image of a resistor.
Note that some of the bytes have an asterisk in front of them? These are the bytes that are inside of the beginPattern and endPattern.
Also note that I used a beginPattern and an endPattern. You do not need to do this, I only did it to make it easier for me to find a pattern in my resistor.png file to test the pattern matching. You can use one variable for both begin and end, set the same value for both or simply assign endPattern = beginPattern if you want to use a single pattern (e.g. "0x31, 0x30, 0x30, 0x31") for the start and finish.