如何切片十六进制数据?
How to slice hex data?
我有十六进制数据,其中存在三个数据,我可以在这些数据上分离十六进制数据中包含的信息:
- ID(2字节)
- 数据包长度(2字节)
- 信息
从数据包的长度,我们可以知道这个十六进制数据中的数据有多长
hex data = 0001001447364B5F48312E305F56312E312E3165000300133836333932313033343330343337310004000838303634000200154D414A3258584D524A32444A363135303900050005010006000843415244000700094341524431000800050000090018383939313035323138303935393533303834300D000A000E706F7274616C6E6D6D73
如果我们根据上面的信息手动分离这个HEX数据那么我们就得到这个数据
0001001447364B5F48312E305F56312E312E3165
这里
id=0001
数据包长度=0014=20
信息=47364B5F48312E305F56312E312E3165
我曾尝试通过我的代码来分隔信息,但它只分隔了第一个数据我想分隔整个十六进制数据
这是我的 python 代码:
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)
谁能帮我算一下
这就是 struct 包的真正用途(和 bytes.fromhex)
>>> import struct
>>> s = "0001001447364B5F48312E305F56312E312E3165000300133836333932313033343330343337310004000838303634000200154D414A3258584D524A32444A363135303900050005010006000843415244000700094341524431000800050000090018383939313035323138303935393533303834300D000A000E706F7274616C6E6D6D73"
>>> mbytes = bytes.fromhex(s)
>>> msg_id,msg_size = struct.unpack(">hh",mbytes[:4])
# 1, 20
>>> msg_bytes = struct.unpack_from("{}s".format(msg_size),mbytes[4:])[0]
>>> msg_hex = bytes.hex(msg_bytes)
# 47364b5f48312e305f56312e312e316500030013
# which i think is what you actually want... but if you actually think size represents each character in that string you will need to make a modification
>>> alt_hex = s[8:][msg_size]
# 47364b5f48312e305f56 ( which i think is the wrong value...)
您可以遍历 data
字符串并提取数据。
这是它的样子:
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
我有十六进制数据,其中存在三个数据,我可以在这些数据上分离十六进制数据中包含的信息:
- ID(2字节)
- 数据包长度(2字节)
- 信息
从数据包的长度,我们可以知道这个十六进制数据中的数据有多长 hex data = 0001001447364B5F48312E305F56312E312E3165000300133836333932313033343330343337310004000838303634000200154D414A3258584D524A32444A363135303900050005010006000843415244000700094341524431000800050000090018383939313035323138303935393533303834300D000A000E706F7274616C6E6D6D73
如果我们根据上面的信息手动分离这个HEX数据那么我们就得到这个数据 0001001447364B5F48312E305F56312E312E3165
这里
id=0001
数据包长度=0014=20
信息=47364B5F48312E305F56312E312E3165
我曾尝试通过我的代码来分隔信息,但它只分隔了第一个数据我想分隔整个十六进制数据
这是我的 python 代码:
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)
谁能帮我算一下
这就是 struct 包的真正用途(和 bytes.fromhex)
>>> import struct
>>> s = "0001001447364B5F48312E305F56312E312E3165000300133836333932313033343330343337310004000838303634000200154D414A3258584D524A32444A363135303900050005010006000843415244000700094341524431000800050000090018383939313035323138303935393533303834300D000A000E706F7274616C6E6D6D73"
>>> mbytes = bytes.fromhex(s)
>>> msg_id,msg_size = struct.unpack(">hh",mbytes[:4])
# 1, 20
>>> msg_bytes = struct.unpack_from("{}s".format(msg_size),mbytes[4:])[0]
>>> msg_hex = bytes.hex(msg_bytes)
# 47364b5f48312e305f56312e312e316500030013
# which i think is what you actually want... but if you actually think size represents each character in that string you will need to make a modification
>>> alt_hex = s[8:][msg_size]
# 47364b5f48312e305f56 ( which i think is the wrong value...)
您可以遍历 data
字符串并提取数据。
这是它的样子:
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