I've been having trouble loading images from a file as a string. Many of the functions that I need to use in my program rely on the read data being encoded with ascii and it simply fails to handle the data I give it producing the following error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa8 in position 14: ordinal not in range(128)
So how would I go about converting this data to ascii.
EDIT:
Here is my admittedly messy code I am using. Please do not comment about how messy it is, this is a rough draft:
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 str2int(string):
binary = text_to_bits(string)
number = int(binary, 2)
return number
def go():
#filen is the name of the file
global filen
#Reading the file
content = str(open(filen, "r").read())
#Using A function from above
integer = str2int(content)
#Write back to the file
w = open(filen, "w").write(str(integer))
strimage data, ask a specific question about that method.