I've recently started using python/pyparsing to process a string of hex values and I'm having trouble with this: Consider this string:
string = "10020304059917368584304025326"
I want the end result to be this:
['10', '02', '03', ['04', '05', '9917365843'], ['04', '02', '5326']]
Assume that 04 is a tag which means data (same concept as in ASN.1), and 05 is the size of that data. I'm not seeing how to use the size variable on the pyparsing code. The best that I can do is:
byte = Word(hexnums, exact=2)
process = byte + byte + byte + Word(hexnums)
newstring = process.parseString(string)
print (newstring.dump())
Any help would be greatly appreciated.
PS: After the help of Hooked, my final code is:
from pyparsing import *
string = "10 02 03 04 05 99 17 36 58 43 04 02 53 26"
tag = Word(hexnums, exact=2)
size = Word(hexnums)
array = Group(tag + countedArray(size))
process = tag + tag + tag + ZeroOrMore(array)
newstring = process.parseString(string)
print (newstring.dump())
Which prints:
['10', '02', '03', ['04', ['99', '17', '36', '58', '43']], ['04', ['53', '26']]]
Hope this helps in the future.