>>> text_to_bits("msg") '011011010111001101100111'
(text, encoding="utf-8", errors="surrogatepass")
| 50 | |
| 51 | # Functions of binary conversion-------------------------------------- |
| 52 | def text_to_bits(text, encoding="utf-8", errors="surrogatepass"): |
| 53 | """ |
| 54 | >>> text_to_bits("msg") |
| 55 | '011011010111001101100111' |
| 56 | """ |
| 57 | bits = bin(int.from_bytes(text.encode(encoding, errors), "big"))[2:] |
| 58 | return bits.zfill(8 * ((len(bits) + 7) // 8)) |
| 59 | |
| 60 | |
| 61 | def text_from_bits(bits, encoding="utf-8", errors="surrogatepass"): |