From 5343ffc83e330cbcb9543261bfd4634e82c4a24d Mon Sep 17 00:00:00 2001 From: bwarsaw Date: Wed, 19 Jul 2000 20:20:39 +0000 Subject: hexlify(), unhexlify(): New helper functions for cookie value encoding and decoding. --- Mailman/Utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Mailman/Utils.py b/Mailman/Utils.py index 4c52042b0..8c7314b5c 100644 --- a/Mailman/Utils.py +++ b/Mailman/Utils.py @@ -609,3 +609,23 @@ def reap(kids, func=None): except KeyError: # Huh? How can this happen? pass + + +# Useful conversion routines +# unhexlify(hexlify(s)) == s +def hexlify(s): + acc = [] + def munge(byte, acc=acc, a=ord('a'), z=ord('0')): + if byte > 9: acc.append(byte+a-10) + else: acc.append(byte+z) + for c in s: + hi, lo = divmod(ord(c), 16) + munge(hi) + munge(lo) + return string.join(map(chr, acc), '') + +def unhexlify(s): + acc = [] + for i in range(0, len(s), 2): + acc.append(chr(int(s[i], 16)*16 + int(s[i+1], 16))) + return string.join(acc, '') -- cgit v1.2.3-70-g09d2