psh
4 months ago
4 changed files with 117 additions and 30 deletions
@ -0,0 +1,90 @@ |
|||||
|
package org.nl.acs.utils; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class ByteUtil{ |
||||
|
|
||||
|
public static List<int[]> splitAndConvertPackets(String data) { |
||||
|
// 将输入的十六进制字符串转换为字节数组
|
||||
|
byte[] dataBytes = hexStringToByteArray(data); |
||||
|
|
||||
|
// 87CD的字节表示
|
||||
|
byte[] startMarker = {(byte) 0x87, (byte) 0xCD}; |
||||
|
|
||||
|
// 存储结果的列表
|
||||
|
List<int[]> packets = new ArrayList<>(); |
||||
|
|
||||
|
// 当前分割点
|
||||
|
int start = 0; |
||||
|
|
||||
|
// 遍历整个字节数组
|
||||
|
while (start < dataBytes.length) { |
||||
|
// 找到下一个87CD的起始位置
|
||||
|
int nextStart = findStartMarker(dataBytes, startMarker, start + 2); |
||||
|
|
||||
|
if (nextStart == -1) { |
||||
|
// 没有找到下一个87CD,说明到了最后一个包
|
||||
|
packets.add(byteArrayToIntArray(dataBytes, start, dataBytes.length - start)); |
||||
|
break; |
||||
|
} else { |
||||
|
// 找到一个新的87CD,将前面的部分作为一个完整的包
|
||||
|
packets.add(byteArrayToIntArray(dataBytes, start, nextStart - start)); |
||||
|
start = nextStart; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return packets; |
||||
|
} |
||||
|
|
||||
|
private static int findStartMarker(byte[] data, byte[] marker, int start) { |
||||
|
for (int i = start; i <= data.length - marker.length; i++) { |
||||
|
boolean match = true; |
||||
|
for (int j = 0; j < marker.length; j++) { |
||||
|
if (data[i + j] != marker[j]) { |
||||
|
match = false; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
if (match) { |
||||
|
return i; |
||||
|
} |
||||
|
} |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
private static byte[] hexStringToByteArray(String s) { |
||||
|
int len = s.length(); |
||||
|
byte[] data = new byte[len / 2]; |
||||
|
for (int i = 0; i < len; i += 2) { |
||||
|
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) |
||||
|
+ Character.digit(s.charAt(i + 1), 16)); |
||||
|
} |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
private static int[] byteArrayToIntArray(byte[] bytes, int offset, int length) { |
||||
|
int[] intArray = new int[length]; |
||||
|
for (int i = 0; i < length; i++) { |
||||
|
intArray[i] = bytes[offset + i] & 0xFF; |
||||
|
} |
||||
|
return intArray; |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
String data = "87cd0008001400010073001000796e030003023e0300000000003f2c87cd00080014000100730010007b6e03000309370200000000003f2d87cd00080014000100730010007f6e03000309310600000000003f3087cd00080014000100730010001f6e030009085c06000001085c3eec87cd00080036000100450032002f00020000000000000000000300000085669788c40000708000000000000000000000000200000a59000000000000000087cd00080036000100450032002f00020000000000000000000300000085669788c40000708000000000000000000000000200000a59000000000000000087cd00080036000100450032002f0006000000000000000000030000012e669788c40000708000000000000000000000000400000000000000000000000087cd00080036000100450032002f00020000000000000000000300000085669788c40000708000000000000000000000000200000a59000000000000000087cd00080036000100450032002f00020000000000000000000300000085669788c50000708000000000000000000000000200000a590000000000000000"; |
||||
|
|
||||
|
// 处理数据
|
||||
|
List<int[]> packets = splitAndConvertPackets(data); |
||||
|
|
||||
|
// 打印分割后的报文
|
||||
|
for (int i = 0; i < packets.size(); i++) { |
||||
|
System.out.print("Packet " + (i + 1) + ": "); |
||||
|
for (int value : packets.get(i)) { |
||||
|
System.out.print(String.format("%02x", value) + " "); |
||||
|
} |
||||
|
System.out.println(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
Loading…
Reference in new issue