Encode 16 binary bytes into a 26-character long base32 string.
    :param binary: Bytestring or list of bytes
    :return: ASCII string of 26 characters
    :rtype: str
180971
def encode_ulid_base32(binary):
    
    assert len(binary) == 16

    if not py3 and isinstance(binary, str):
        binary = [ord(b) for b in binary]

    symbols = _symbols
    return ''.join([
        symbols[(binary[0] & 224) >> 5],
        symbols[binary[0] & 31],
        symbols[(binary[1] & 248) >> 3],
        symbols[((binary[1] & 7) << 2) | ((binary[2] & 192) >> 6)],
        symbols[(binary[2] & 62) >> 1],
        symbols[((binary[2] & 1) << 4) | ((binary[3] & 240) >> 4)],
        symbols[((binary[3] & 15) << 1) | ((binary[4] & 128) >> 7)],
        symbols[(binary[4] & 124) >> 2],
        symbols[((binary[4] & 3) << 3) | ((binary[5] & 224) >> 5)],
        symbols[binary[5] & 31],
        symbols[(binary[6] & 248) >> 3],
        symbols[((binary[6] & 7) << 2) | ((binary[7] & 192) >> 6)],
        symbols[(binary[7] & 62) >> 1],
        symbols[((binary[7] & 1) << 4) | ((binary[8] & 240) >> 4)],
        symbols[((binary[8] & 15) << 1) | ((binary[9] & 128) >> 7)],
        symbols[(binary[9] & 124) >> 2],
        symbols[((binary[9] & 3) << 3) | ((binary[10] & 224) >> 5)],
        symbols[binary[10] & 31],
        symbols[(binary[11] & 248) >> 3],
        symbols[((binary[11] & 7) << 2) | ((binary[12] & 192) >> 6)],
        symbols[(binary[12] & 62) >> 1],
        symbols[((binary[12] & 1) << 4) | ((binary[13] & 240) >> 4)],
        symbols[((binary[13] & 15) << 1) | ((binary[14] & 128) >> 7)],
        symbols[(binary[14] & 124) >> 2],
        symbols[((binary[14] & 3) << 3) | ((binary[15] & 224) >> 5)],
        symbols[binary[15] & 31],
    ])
197293
def encode(data: Union[str, bytes]) -> str:
        
        return ensure_str(base58.b58encode(ensure_bytes(data)))
1299019
def to_binary(value, encoding='utf-8'):
    
    if not value:
        return b''
    if isinstance(value, six.binary_type):
        return value
    if isinstance(value, six.text_type):
        return value.encode(encoding)
    return to_text(value).encode(encoding)
1162510
def _bytes_to_uint_48(self, bytes_):
        
        return ((bytes_[0] * pow(2, 40)) + (bytes_[1] * pow(2, 32)) +
                (bytes_[2] * pow(2, 24)) + (bytes_[3] << 16) +
                (bytes_[4] << 8) + bytes_[4])
694511
def int_to_bytes(val, bit=32, signed=False, big_endian=True):
    
    val = int(val) #ensure it is an int

    if six.PY3:
        order = 'little'
        if big_endian:
            order = 'big'
        return val.to_bytes(length=bit//8, byteorder=order, signed=signed)

    if bit == 8:
        code = 'B'
    elif bit == 16:
        code = 'H'
    elif bit == 32:
        code = 'I'
    elif bit == 64:
        code = 'Q'
    else:
        raise Exception("int_to_bytes : size parameter value should be 8, 16, 32, or 64")

    if big_endian:
        code = '>'+code
    else:
        code = '<'+code

    if signed or val < 0:
        code = code.lower()

    return struct.pack(code, val)
1163616
def encode(hex)
      leading_zero_bytes = (hex.match(/^([0]+)/) ? $1 : '').size / 2
      int_val = hex.to_i(16)
      base58_val = ''
      while int_val > 0
        int_val, remainder = int_val.divmod(SIZE)
        base58_val = ALPHABET[remainder] + base58_val
      end
      ('1' * leading_zero_bytes) + base58_val
    end
862824
def py_encode_basestring_ascii(s, _PY3=PY3):
    
    if _PY3:
        if isinstance(s, binary_type):
            s = s.decode('utf-8')
    else:
        if isinstance(s, str) and HAS_UTF8.search(s) is not None:
            s = s.decode('utf-8')
    def replace(match):
        s = match.group(0)
        try:
            return ESCAPE_DCT[s]
        except KeyError:
            n = ord(s)
            if n < 0x10000:
                #return '\\u{0:04x}'.format(n)
                return '\\u%04x' % (n,)
            else:
                # surrogate pair
                n -= 0x10000
                s1 = 0xd800 | ((n >> 10) & 0x3ff)
                s2 = 0xdc00 | (n & 0x3ff)
                #return '\\u{0:04x}\\u{1:04x}'.format(s1, s2)
                return '\\u%04x\\u%04x' % (s1, s2)
    return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"'
80427
def binary_encode_if_any_high_ascii(string)
      string = ensure_utf8(string)
      string.force_encoding('BINARY') if string.bytes.detect { |byte| byte > 127 }
      string
    end
755525
def byte2bit_string(data):
    
    if isinstance(data, basestring):
        assert len(data) == 1
        data = ord(data)

    bits = '{0:08b}'.format(data)
    bits = bits[::-1]
    return bits
1266743
def base256_encode(n, minwidth=0):  # int/long to byte array
    
    if n > 0:
        arr = []
        while n:
            n, rem = divmod(n, 256)
            arr.append(rem)
        b = bytearray(reversed(arr))
    elif n == 0:
        b = bytearray(b'\x00')
    else:
        raise ValueError("Negative numbers not supported")

    if minwidth > 0 and len(b) < minwidth:  # zero padding needed?
        padding = (minwidth - len(b)) * b'\x00'
        b = bytearray(padding) + b
    b.reverse()

    return b
539266
def _decode16(self, offset):
        
        str_len, skip = self._decode_length(offset, 2)
        offset += skip

        # The len is the string len in utf-16 units
        encoded_bytes = str_len * 2

        data = self.m_charbuff[offset: offset + encoded_bytes]

        assert self.m_charbuff[offset + encoded_bytes:offset + encoded_bytes + 2] == b"\x00\x00", \
            "UTF-16 String is not null terminated! At offset={}".format(offset)

        return self._decode_bytes(data, 'utf-16', str_len)
1037422
def encode_base64(msg):
    
    orig = msg.get_payload()
    encdata = str(_bencode(orig), 'ascii')
    msg.set_payload(encdata)
    msg['Content-Transfer-Encoding'] = 'base64'
964459
public static String encode(byte[] binaryData) {
        if (binaryData.length != 16)
            return null;

        char[] buffer = new char[32];

        for (int i = 0; i < 16; i++) {
            int low = (int) (binaryData[i] & 0x0f);
            int high = (int) ((binaryData[i] & 0xf0) >> 4);
            buffer[i * 2] = hexadecimal[high];
            buffer[i * 2 + 1] = hexadecimal[low];
        }

        return new String(buffer);

    }
64186
def encode_ber(value, ber_length=0):
    
    if not ber_length:
        if value < 127: # 1 byte case
            return bytearray([value])
        ber_length = 1
        while value >= pow(2, 8*ber_length):
            ber_length += 1
        ber_length += 1


    ber = bytearray(ber_length)
    ber[0] = 127 + ber_length #ber length byte
    for i in range(1, ber_length):
        ber[i] = (value >> (8 * (ber_length - 1 - i))) & 255
    return ber
1364700
def to_binary_string(obj, encoding=None):
    """"""
    if PY2:
        # Python 2
        if encoding is None:
            return str(obj)
        else:
            return obj.encode(encoding)
    else:
        # Python 3
        return bytes(obj, 'utf-8' if encoding is None else encoding)
914983
def bin2str(b):
    
    ret = []
    for pos in range(0, len(b), 8):
        ret.append(chr(int(b[pos:pos + 8], 2)))
    return ''.join(ret)
809430
def byte_to_bitstring(byte):
    """"""
    assert 0 <= byte <= 0xff
    bits = [int(x) for x in list(bin(byte + 0x100)[3:])]
    return bits