Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在多个字节中查找特定字节?;_Java_Byte_Hexdump - Fatal编程技术网

Java 如何在多个字节中查找特定字节?;

Java 如何在多个字节中查找特定字节?;,java,byte,hexdump,Java,Byte,Hexdump,我使用Java读取了一个文件,并使用HexDump输出数据。看起来是这样的: 第一行和第二行: 一:31 30 30 31 30 31 31 30 31 31 31 二:30 31 31 30 31 31 30 31 31 31 31 30 31 31 31 31 31 31 我想打印第一个“31 30 31”和第二个“31 30 31”之间的数据。我理想的输出是31 30 30 31 30 31 31 31 31 30 31. 但是实际输出是错误的,我想我的代码在数据1中找不到31。如何找出它

我使用Java读取了一个文件,并使用HexDump输出数据。看起来是这样的: 第一行和第二行: 一:31 30 30 31 30 31 31 30 31 31 31 二:30 31 31 30 31 31 30 31 31 31 31 30 31 31 31 31 31 31 我想打印第一个“31 30 31”和第二个“31 30 31”之间的数据。我理想的输出是31 30 30 31 30 31 31 31 31 30 31. 但是实际输出是错误的,我想我的代码在数据1中找不到31。如何找出它

我使用JDK1.7,软件是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);
        }

    }
}


我的理想输出是31 30 30 30 31 30 31 31 31 31 31 31 31 31 30 31. 但实际产出是:

我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它

我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它

我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它

我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它

我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它 我找不到它
30找不到它

我觉得您的输入数据有点混乱。然而,这可能回答了你的问题

它不会给出与您要求的完全相同的输出,但是我认为您应该能够通过使用标志“inPattern”来调整它以打开或关闭输出。如果inPattern为true,则打印从文件读取的数据;如果为false,则不要打印从文件读取的数据

这可能不是最好的编码形式,因为它完全是静态方法——但它满足了您的要求

您的代码(我认为)的问题是data1将是一个2个字符的字符串。它不可能包含11个字符的字符串(“31 30 31”)。如果您尝试反转测试(即“31 30 31”.contains(data1)),那么它将只匹配一个字节,而不是您打算匹配的4个字节

包转储;
导入java.io.DataInputStream;
导入java.io.FileInputStream;
导入java.io.InputStream;
导入java.util.LinkedList;
公共类hextumpWithFilter{
//私有静态final int beginPattern[]={0x47,0x0d,0x0a,0x1a};
私有静态final int beginPattern[]={0x00,0x83,0x7d,0x8a};
私有静态final int-endPattern[]={0x23,0x01,0x78,0xa5};
private static LinkedList bytesRead=new LinkedList();
公共静态void main(字符串[]args){
试一试{
InputStream isr=新数据InputStream(新文件InputStream(“C:\\Temp\\restort.png”);
int bytesPerLine=16;
int字节数=0;
int数据;
布尔inPattern=false;
而((data=isr.read())!=-1){
//捕获刚刚读入输入缓冲区的数据。
bytesRead.add(数据);
//如果输入缓冲区中的数据太多,无法与
//模式,剥离第一个字节。
//注意:这假设开始模式和结束模式的长度相同。
if(bytesRead.size()>beginPattern.length){
bytesRead.removeFirst();
}
//在每一行新的输出开始处输出一个字节计数。
if(字节计数%bytesPerLine==0)
System.out.printf(“\n%04x:”,字节数);
//输出间距-如果我们找到了图案,那么也输出一个星号
系统输出打印F(输入模式?*%02x):“%02x”,数据);
//最后检查是否找到了模式,是否有足够的字节
//在字节读取缓冲区中。
if(byteRead.size()==beginPattern.length){
//如果当前未处于模式中,则检查开始模式
if(!inPattern&&checkPattern(beginPattern,bytesRead)){
inPattern=true;
}
//如果当前处于模式中,则检查结束模式。
if(inPattern&&checkPattern(endPattern,bytesRead)){
inPattern=false;
}
}
字节数+=1;
}
System.out.println();
}捕获(例外e){
System.out.println(“异常:+e”);
}
}
/**
*函数检查从文件读取的输入缓冲区是否匹配
*提供的模式。
*@param pattern要在缓冲区中查找的模式。
*@param bytesRead从文件读取的字节缓冲区。
*@return true,如果pattern和bytesRead的内容相同。
*/
私有静态布尔检查模式(int[]模式,LinkedList字节读取){
int-ptr=0;
布尔模式匹配=真;
for(int br:bytesRead){
if(br!=模式[ptr++]){
拍拍
0000:  89  50  4e  47  0d  0a  1a  0a  00  00  00  0d  49  48  44  52
0010:  00  00  00  60  00  00  00  1b  08  06  00  00  00  83  7d  8a
0020: *3a *00 *00 *00 *09 *70 *48 *59 *73 *00 *00 *2e *23 *00 *00 *2e
0030: *23 *01 *78 *a5  3f  76  00  00  00  07  74  49  4d  45  07  e3
0040:  03  0e  17  1a  0f  c2  80  9c  d0  00  00  01  09  49  44  41
0050:  54  68  de  ed  9a  31  0b  82  40  18  86  cf  52  d4  a1  7e
0060:  45  4e  81  5b  a3  9b  10  ae  ae  4d  4d  61  7f  a1  21  1b
0070:  fa  0b  45  53  53  ab  ab  04  6e  42  4b  9b  d0  64  bf  a2
0080:  06  15  a9  6b  ef  14  82  ea  ec  e8  7d  c6  f7  0e  f1  be
0090:  e7  3b  0f  0e  25  4a  29  25  a0  31  5a  28  01  04  fc  35
00a0:  f2  73  e0  af  af  b5  93  fd  c9  8c  cd  36  cb  da  f9  ae
00b0:  ad  11  d3  50  84  2e  50  92  96  24  88  f2  ca  b1  41  7b
00c0:  cc  64  c7  db  b6  be  7e  5e  87  ef  0e  08  e3  82  64  85
00d0:  b8  47  4c  56  50  12  c6  85  b8  9f  20  1e  0b  10  bd  81
00e0:  64  1e  5b  38  49  cb  ca  31  e3  7c  67  b2  b4  c7  f6  c4
00f0:  62  da  65  b2  f9  ea  c2  64  a7  dd  90  c9  fa  a3  3d  0e
0100:  61  00  01  10  00  20  00  02  00  04  40  00  80  00  08  00
0110:  10  00  01  00  02  7e  82  af  5f  c6  99  86  42  5c  5b  7b
0120:  eb  19  be  f7  e2  8d  a4  77  f8  e8  bb  07  51  5e  7b  91
0130:  28  c4  0e  d0  55  89  38  96  2a  6c  77  3a  96  4a  74  55
0140:  12  57  00  8f  05  88  de  40  12  fe  8a  c0  21  0c  01  00
0150:  02  20  00  34  c3  03  f7  3f  46  9a  04  49  f8  9d  00  00
0160:  00  00  49  45  4e  44  ae  42  60  82