Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
- bits = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:]
- return bits.zfill(8 * ((len(bits) + 7) // 8))
- def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
- n = int(bits, 2)
- try :
- return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode(encoding, errors) or '