简体   繁体   中英

How to pick out bytes from the string of bytes?

I have a payload from a pcap that looks like:

b'01005e1cd039f4cc55a050000800450000b403ef4000fa1111fd3ff7707fe99cd03907054e5900a01f353d0b00026f2e000100000172a5c12c8043'

How can I pick out bytes from a byte offset? For example, if I want the 4th to 7th bytes?

Just use python standard slicing:

To get the 4th to 7th byte, simply do

bytes_str = b'01005e1cd039f4cc55a050000800450000b403ef4000fa1111fd3ff7707fe99cd03907054e5900a01f353d0b00026f2e000100000172a5c12c8043'
print(bytes_str[4:7+1]) # (we add one because the stop value is exclusive)

and the output

b'5e1c'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM