summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2014-10-02 22:11:39 +0200
committerJan Pokorný <jpokorny@redhat.com>2014-10-02 22:11:39 +0200
commit01df9960e6bd4a99d982eae32182d86146498812 (patch)
treeee2475ed3b08a40c89527673ce1e85e54ac2fea0
parentd6a4f12955180d99c85edb853166adb7791f856f (diff)
downloadsnippets-01df9960e6bd4a99d982eae32182d86146498812.tar.gz
snippets-01df9960e6bd4a99d982eae32182d86146498812.tar.xz
snippets-01df9960e6bd4a99d982eae32182d86146498812.zip
Add python/001-hexstring2bytes.py
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
-rw-r--r--python/001-hexstring2bytes.py11
1 files changed, 11 insertions, 0 deletions
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'))),
+ ''
+ )