LapTux
LapTux

Reputation: 21

Extracting a Part of a Hex-encoded String

Given the string: aa0a051c1400, how would I go about extracting just 05 or the 1c? I'm reading serial responses and I need to examine fragments of hex data to decipher what how much data is coming down the pipe after this initial data packet.

Upvotes: 1

Views: 815

Answers (1)

mattexx
mattexx

Reputation: 6606

Just parsing the string by index works fine. If you want to parse by character index:

from binascii import hexlify, unhexlify

def get_hex(stream, idx):
    return hexlify(unhexlify(stream)[idx])

get_hex('aa0a051c1400', 0)
'aa'

Upvotes: 1

Related Questions