J
J
jack_ganakock2016-01-11 13:14:25
Python
jack_ganakock, 2016-01-11 13:14:25

Python 3.4 What's the fastest way to pack a string in 7bit?

def encode_text(text, bit7):
    if bit7:
        res = list()
        while text:
            d = list()
            r = list() 
 
            for i in range(len(text[:8])):
                b = '{0:07b}'.format(ord(text[i]))
                if not i:
                    r.append(b)
                else:
                    r.append(b[:-i])
                    d.append(b[-i:])
                
            

            if '' in r:
                r.remove('')

            for i in range(len(r)):
                try:
                    res.append(d[i] + r[i])
                except:
                    res.append('{0:08b}'.format(int(r[i],2)))


            text = text[8:]
        return res

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-01-11
@sim3x

In [1]: print("лол".encode('utf-8'))
b'\xd0\xbb\xd0\xbe\xd0\xbb'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question