2010-11-28 58 views
0

我试图找到答案,但现在是凌晨1点,我有点累了。如何扫描Java中的十六进制字符串数据库文件

请问有人可以指点我正确的方向,我想要做的是扫描一个文件使用Java(.wmdb数据库文件)的十六进制值的模式,然后拉出这个模式后的十六进制值。

例如 1.扫描为一个十六进制值 “6B 4A 80 10 00 00 00 00 00” 2.拉出的十六进制值,直到达到扫描器的 “00 00 28” 3.重复

的值

我敢肯定,有一个简单的方法,我错过了使用二进制IO,但我似乎无法工作。虽然我没有接受答案,但正确的方向或一个简单的例子会帮助我很多。

回答

0

你是对的,有一个简单的方法来做到这一点。只需将您的开始匹配和结束匹配表示转换为byte s的序列即可。然后搜索java.io.InputStream中的开始序列,并提取值直到匹配结束匹配。对于匹配,您可以使用well-known algorithms之一。

这里的例子天真的实施,将收集beginMatch和endMatch之间的所有字节序列:

public ArrayList<ArrayList<Integer>> pullOutBytes(InputStream stream, ArrayList<Integer> beginMatch, ArrayList<Integer> endMatch) 
     throws IOException { 
    ArrayList<ArrayList<Integer>> pulledOut = new ArrayList<ArrayList<Integer>>(); 
    int b; 
    BeginSearch: 
    while ((b = stream.read()) != -1) { 
     int beginMatchIndex = 0; 
     if (b != beginMatch.get(beginMatchIndex)) { 
      continue BeginSearch; 
     } 
     beginMatchIndex++; 
     while ((b = stream.read()) != -1 && beginMatchIndex < beginMatch.size()) { 
      if (b != beginMatch.get(beginMatchIndex)) { 
       continue BeginSearch; 
      } 
      beginMatchIndex++; 
     } 
     if (beginMatchIndex < beginMatch.size()) { 
      break; 
     } 
     int endMatchIndex = 0; 
     ArrayList<Integer> pull = new ArrayList<Integer>(); 
     pull.add(b); 
     while ((b = stream.read()) != -1) { 
      pull.add(b); 
      if (b == endMatch.get(endMatchIndex)) { 
       if (++endMatchIndex == (endMatch.size() - 1)) { 
        while (endMatchIndex > 0) { 
         pull.remove(pull.size() - 1); 
         endMatchIndex--; 
        } 
        pulledOut.add(pull); 
        continue BeginSearch; 
       } 
      } 
     } 
    } 
    return pulledOut; 
} 
相关问题