0

I am having quite the difficult time parsing this .dat file using Java. The format of the .dat file which I am trying to parse is shown below. These are actually packets for a packet generator program I am working on. Each packet is delineated with a new line between each block. I want each byte in the packet to be stored in a single byte []. I have been using the BufferedReader readline() method to go line by line but still have had little success in storing an entire packet. Thanks in advance for any help you can provide.

00 04 75 8d 49 c7 00 01 03 cd 50 3c 08 00 45 00 
00 30 08 0d 40 00 80 06 00 00 c0 a8 ec 20 c0 a8 
ec 1e 04 16 00 50 b8 63 45 fd 00 00 00 00 70 02 
fa f0 2b d7 00 00 02 04 05 b4 01 01 04 02 

00 01 03 cd 50 3c 00 01 03 dd 4c 2d 08 00 45 00 
00 30 00 00 40 00 40 06 e1 37 c0 a8 ec 1e c0 a8 
ec 20 00 50 04 16 e9 bb 68 bf b8 63 45 fe 70 12 
16 d0 bd 6b 00 00 02 04 05 b4 01 01 04 02 

00 04 75 8d 49 c7 00 01 03 cd 50 3c 08 00 45 00 
00 28 08 0f 40 00 80 06 00 00 c0 a8 ec 20 c0 a8 
ec 1e 04 16 00 50 b8 63 45 fe e9 bb 68 c0 50 10 
fa f0 59 ab 00 00 

00 04 75 8d 49 c7 00 01 03 cd 50 3c 08 00 45 00 
00 ed 08 10 40 00 80 06 00 00 c0 a8 ec 20 c0 a8 
ec 1e 04 16 00 50 b8 63 45 fe e9 bb 68 c0 50 18 
fa f0 5a 70 00 00 47 45 54 20 2f 20 48 54 54 50 
2f 31 2e 31 0d 0a 41 63 63 65 70 74 3a 20 2a 2f 
2a 0d 0a 41 63 63 65 70 74 2d 4c 61 6e 67 75 61 
67 65 3a 20 65 6e 2d 75 73 0d 0a 41 63 63 65 70 
74 2d 45 6e 63 6f 64 69 6e 67 3a 20 67 7a 69 70 
2c 20 64 65 66 6c 61 74 65 0d 0a 55 73 65 72 2d 
41 67 65 6e 74 3a 20 4d 6f 7a 69 6c 6c 61 2f 34 
2e 30 20 28 63 6f 6d 70 61 74 69 62 6c 65 3b 20 
4d 53 49 45 20 36 2e 30 3b 20 57 69 6e 64 6f 77 
73 20 4e 54 20 35 2e 30 29 0d 0a 48 6f 73 74 3a 
20 31 39 32 2e 31 36 38 2e 32 33 36 2e 33 30 0d 
0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 
70 2d 41 6c 69 76 65 0d 0a 0d 0a 
4

1 回答 1

0

数组是固定长度的,看起来你不知道你将拥有多少字节,直到你遇到一个空行,你不知道你将拥有多少个数据包。

我建议使用 anArrayList<ArrayList<Byte>>来保存所有数据包 - 这将根据需要增长。

创建一个初始ArrayList<Byte>然后这是一个简单的循环:

  • 虽然readline()不为空
  • 检查空行
  • 如果不是空白
    • 解析StringString[]使用String.split()
    • 遍历String[]并将每个十六进制对转换为 abyte并添加到ArrayList<Btye>
  • 如果为空
    • 添加ArrayList<Byte>ArrayList<ArrayList<Byte>>
    • 创建新的ArrayList<Byte>

readline()返回时null循环退出并且您ArrayList<ArrayList<Byte>>包含所有数据包。

于 2013-02-14T04:46:45.953 回答