I'm trying to convert string to binary and take one's complement, after that display the string again. i have seen a couples of related post such as here and here and i'm follow the official work where have been posted in here, in the below code after run the code its showing error AttributeError: 'bytes' object has no attribute 'encode'. i'm using python 3.6
the below code is :
import binascii
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]
return bits.zfill(8 * ((len(bits) + 7) // 8))
def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
n = int(bits, 2)
return int2bytes(n).decode(encoding, errors)
def int2bytes(i):
hex_string = '%x' % i
n = len(hex_string)
return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
your_string='hello'
b=your_string.encode('ascii', 'strict')
text_to_bits(b)
is there a way after convert it to binary to take one's complement of it and display the string again?