Python - NumPy array splitting in particuler indices

Viewed 17

I have a binary file containing multiple UDP packets received from a server. Each UDP packet starts with 0xAAAA and ends with 0xD6D6 (start marker and end marker). I read the file and store it in a NumPy array. Now I have to split that into multiple smaller arrays corresponding to individual packets.

I tried np.array_split, but it gives n chunks of the same size. However, what I need is to get it split at every 0xD6D6 and 0xAAAA points.

1 Answers

I think you'll have to use a for-loop since the size of each packet is not guaranteed to be the same size:

packets_data = np.array()  # This should be your packets array of shape(data_length,)

individual_packets = []
one_packet = []
for data in packets_data:
    one_packet.append(data)
    if data == "0xD6D6":
        individual_packets.append(one_packet[:])
        one_packet = []
Related