MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / text_to_bits

Function text_to_bits

hashes/hamming_code.py:52–58  ·  view source on GitHub ↗

>>> text_to_bits("msg") '011011010111001101100111'

(text, encoding="utf-8", errors="surrogatepass")

Source from the content-addressed store, hash-verified

50
51# Functions of binary conversion--------------------------------------
52def 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
61def text_from_bits(bits, encoding="utf-8", errors="surrogatepass"):

Callers

nothing calls this directly

Calls 1

encodeMethod · 0.45

Tested by

no test coverage detected