From 01df9960e6bd4a99d982eae32182d86146498812 Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Thu, 2 Oct 2014 22:11:39 +0200 Subject: Add python/001-hexstring2bytes.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- python/001-hexstring2bytes.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 python/001-hexstring2bytes.py (limited to 'python/001-hexstring2bytes.py') diff --git a/python/001-hexstring2bytes.py b/python/001-hexstring2bytes.py new file mode 100644 index 0000000..22de43d --- /dev/null +++ b/python/001-hexstring2bytes.py @@ -0,0 +1,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'))), + '' + ) -- cgit