Convert binary to base58 using BASE58_ALPHABET. Like Bitcoin addresses.
334209
def b2a_base58(s):
    """"""
    v, prefix = to_long(256, lambda x: x, iterbytes(s))
    s = from_long(v, prefix, BASE58_BASE, lambda v: BASE58_ALPHABET[v])
    return s.decode("utf8")
1091660
def b58decode(s, errors='strict'):
    ""
    if not s:
        return (b'', 0)

    # Convert the string to an integer
    n = 0
    for c in s:
        n *= 58
        if c not in b58digits:
            raise InvalidBase58Error(u"character %r is not a valid base58 "
                u"character" % c)
        digit = b58digits.index(c)
        n += digit

    # Convert the integer to bytes
    res = BigInteger(n).serialize((n.bit_length()+7)//8 or 1)

    # Add padding back.
    pad = 0
    for c in s[:-1]:
        if c == b58digits[0]: pad += 1
        else: break
    return (b'\x00' * pad + res, len(s))
777331
def b58encode(v: bytes) -> str:
    ''''''

    origlen = len(v)
    v = v.lstrip(b'\0')
    newlen = len(v)

    p, acc = 1, 0
    for c in iseq(v[::-1]):
        acc += p * c
        p = p << 8

    result = ''
    while acc > 0:
        acc, mod = divmod(acc, 58)
        result += alphabet[mod]

    return (result + alphabet[0] * (origlen - newlen))[::-1]
187016
def to_hex(x):
    
    if isinstance(x, bytearray):
        x = bytes(x)
    elif isinstance(x, (list, tuple)):
        x = bytes(bytearray(x))
    if isinstance(x, basestring):
        return base64.b16encode(x).decode('ascii')
    else:
        raise ValueError('Unknown input argument type')
939939
def bin_to_int(string):
    """"""
    if isinstance(string, str):
        return struct.unpack("b", string)[0]
    else:
        return struct.unpack("b", bytes([string]))[0]
1153468
def b58dec(enc_uid):
    ''''''
    if isinstance(enc_uid, str):
        pass
    elif isinstance(enc_uid, bytes):
        enc_uid = enc_uid.decode('utf8')
    else:
        raise ValueError('Cannot decode this type: {}'.format(enc_uid))
    uid = 0
    try:
        for i, ch in enumerate(enc_uid):
            uid = (uid * 58) + BASE58INDEX[ch]
    except KeyError:
        raise ValueError('Invalid character: "{}" ("{}", index 5)'.format(ch, enc_uid, i))
    return uid
985959
protected static function base58_decode($base58)
    {
        $origbase58 = $base58;
        $return = "0";
        for ($i = 0; $i < strlen($base58); $i++) {
            $return = gmp_add(gmp_mul($return, 58), strpos(self::$B58_DIGITS, $base58[$i]));
        }
        $return = gmp_strval($return, 16);
        for ($i = 0; $i < strlen($origbase58) && $origbase58[$i] == "1"; $i++) {
            $return = "00" . $return;
        }
        if (strlen($return) % 2 != 0) {
            $return = "0" . $return;
        }
        return $return;
    }
1296482
def address_to_oa_address(btc_address)
      begin
        btc_hex = decode_base58(btc_address)
        btc_hex = '0' +btc_hex if btc_hex.size==47
        address = btc_hex[0..-9] # bitcoin address without checksum
        named_addr = OA_NAMESPACE.to_s(16) + address
        oa_checksum = checksum(named_addr)
        encode_base58(named_addr + oa_checksum)
      rescue ArgumentError
        nil # bech32 format fails to decode. TODO define OA address for segwit
      end
    end
1325198
def LEB128toint(LEBinput):
    '''
    
    '''

    reversedbytes = hexreverse(LEBinput)
    binstr = ""
    for i in range(len(LEBinput) // 2):
        if i == 0:
            assert int(reversedbytes[2*i:(2*i + 2)],16) < 128
        else:
            assert int(reversedbytes[2*i:(2*i + 2)],16) >= 128
        tempbin = str(bin(int(reversedbytes[2*i:(2*i + 2)],16))) \
                  .lstrip("0b").replace("b","").replace("L","") \
                  .replace("'","").replace('"',"") \
                  .zfill(8)
        binstr += tempbin[1:]
    return int(binstr,2)
1202092
def encode_base58_address(hex, addr_version)
      base = addr_version + hex
      Base58.encode(base + calc_checksum(base))
    end
1056033
def decode(s, checksum=True):
    """"""
    v, prefix = to_long(
        BASE58_BASE, lambda c: BASE58_LOOKUP[c], s.encode("utf8"))

    data = from_long(v, prefix, 256, lambda x: x)

    if checksum:
        data, the_hash = data[:-4], data[-4:]
        if utils.hash256(data)[:4] == the_hash:
            return data
        raise ValueError("hashed base58 has bad checksum %s" % s)

    return data
632245
def decode_bu64(b):
    
    s = b
    s = s.replace(b'-', b'+')
    s = s.replace(b'_', b'/')
    p = len(s) % 4
    if p == 0:
        pass
    elif p == 2:
        s += b'=='
    elif p == 3:
        s += b'='
    else:
        raise ValueError('Illegal Base64url string')
    return base64.standard_b64decode(s)
1372354
private static byte[] parseAsHexOrBase58(String data) {
        try {
            return Utils.HEX.decode(data);
        } catch (Exception e) {
            // Didn't decode as hex, try base58.
            try {
                return Base58.decodeChecked(data);
            } catch (AddressFormatException e1) {
                return null;
            }
        }
    }
19946
def address_as_b58(addr):
    
    if is_c32_address(addr):
        return c32ToB58(addr)

    else:
        if check_address(addr):
            return addr
        else:
            raise ValueError('Address {} is not b58 or c32'.format(addr))
143142
def base36(value):
    
    result = ''
    while value:
        value, i = divmod(value, 36)
        result = BASE36_ALPHABET[i] + result
    return result
516645
def _to_binary(val):
    """"""
    if isinstance(val, text_type):
        return val.encode('utf-8')
    assert isinstance(val, binary_type)
    return val
150570
def encode(data, checksum=True):
    """"""

    if checksum:
        data = data + utils.hash256(data)[:4]
    v, prefix = to_long(256, lambda x: x, iter(data))
    data = from_long(v, prefix, BASE58_BASE, lambda v: BASE58_ALPHABET[v])
    return data.decode("utf8")