summaryrefslogtreecommitdiffstats
path: root/python/001-hexstring2bytes.py
blob: 22de43d056a51e1dd28d85fc13dc037b7d9a45fb (plain)
1
2
3
4
5
6
7
8
9
10
11
# we only consider absolute value (for tha case of "-0x..." hexstr parameter)
# presumably damn slow, but should work for magic strings with uknown length
hexstring2bytes = \
    lambda hexstr: \
        reduce(
            lambda acc, (i, x):
                acc[:-1] + chr(int(x + hex(ord(acc[-1]))[-1], 16)) if i % 2
                else acc + chr(int(x, 16)),
            enumerate(reversed(hexstr.lstrip('-0x'))),
            ''
        )