I have and Hex data in which there are three data present on which I can separate the information contain in the hex data:
- ID (2 bytes)
- length of the packet(2 bytes)
- information
from the length of the packet, we come to know how long is the data in this hex data for example hex data = 0001001447364B5F48312E305F56312E312E3165000300133836333932313033343330343337310004000838303634000200154D414A3258584D524A32444A363135303900050005010006000843415244000700094341524431000800050000090018383939313035323138303935393533303834300D000A000E706F7274616C6E6D6D73
if we manually separate this HEX data according to the above information then we get this data 0001001447364B5F48312E305F56312E312E3165
here
id=0001
packet lenght=0014=20
information = 47364B5F48312E305F56312E312E3165
i have tried to separate the information by my code but it only separates the first data I want to separate the whole hex data
this is my python code:
data="0001001447364B5F48312E305F56312E312E316500030013383633393231303334333034333731"
t=data[0:4]
l=data[4:8]
hex_to_decimal=int(l, 16)
data1=data[0:hex_to_decimal*2]
print(data1)
can anyone help me to figure it up
Advertisement
Answer
You can loop over the data
string and extract the data.
Here is how it would look like:
data="0001001447364B5F48312E305F56312E312E3165000300133836333932313033343330343337310004000838303634000200154D414A3258584D524A32444A363135303900050005010006000843415244000700094341524431000800050000090018383939313035323138303935393533303834300D000A000E706F7274616C6E6D6D73"
i = 0
while i < len(data):
t = data[i:i+4]
l = data[i+4:i+8]
hex_to_decimal = int(l, 16)
data1 = data[i:i+hex_to_decimal*2]
print(f'Length: {hex_to_decimal*2}nData: {data1}n')
i += hex_to_decimal*2
Output:
Length: 40
Data: 0001001447364B5F48312E305F56312E312E3165
Length: 38
Data: 00030013383633393231303334333034333731
Length: 16
Data: 0004000838303634
Length: 42
Data: 000200154D414A3258584D524A32444A3631353039
Length: 10
Data: 0005000501
Length: 16
Data: 0006000843415244
Length: 18
Data: 000700094341524431
Length: 10
Data: 0008000500
Length: 48
Data: 00090018383939313035323138303935393533303834300D
Length: 28
Data: 000A000E706F7274616C6E6D6D73