示例#1
0
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
示例#2
0
def to_str(x):
    if isv3() and isinstance(x, bytes):
        s = ''
        for b in x:
            s += chr(b)
        x = s
    return x
示例#3
0
def to_bytes(x):
    if isv3() and isinstance(x, str):
        xs = []
        for c in x:
            xs.append(ord(c))
        x = bytes(xs)
    return x
示例#4
0
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)
示例#5
0
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)
示例#6
0
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")