def to_hex(x): s = None if isinstance(x, int) or (not isv3() and isinstance(x, long)): s = hex(x)[2:].rstrip('L') elif isv3(): from binascii import hexlify s = hexlify(to_bytes(x)).decode('ascii') else: s = x.encode("hex") return ('0' + s) if len(s) % 2 == 1 else s
def to_str(x): if isv3() and isinstance(x, bytes): s = '' for b in x: s += chr(b) x = s return x
def to_bytes(x): if isv3() and isinstance(x, str): xs = [] for c in x: xs.append(ord(c)) x = bytes(xs) return x
def z85dec(src): if isv3() and isinstance(src, bytes): src = b2s(src) src += '0' * ((5 - len(src)) % 5) lookup = {} for i, c in enumerate(z85_chars): lookup[c] = i xs = [] for i, c in enumerate(src): if i % 5 == 0: x = 0 x = x * 85 + lookup[c] if i % 5 == 4: xs.append(x) return struct.pack('>{}I'.format(len(xs)), *xs)
def as_int(x): if isv3(): from binascii import hexlify return int(hexlify(to_bytes(x)).decode('ascii'), 16) else: return int(x.encode("hex"), 16)
def as_str(x): if isv3(): from binascii import unhexlify return to_str(unhexlify(to_bytes(to_hex(x)))) else: return to_hex(x).decode("hex")