python - Why do I get the error -
# read original bitmap file, goal encode message in original bitmap file , output encoded bitmap file infile=open("c:\users\livio\desktop\imo2015.bmp","rb") header=infile.read(54) body=infile.read() message=open("c:\users\livio\desktop\honourable mention - imo 2014.pdf","rb") messagecontent=message.read() outfile=open("c:\users\livio\desktop\output.bmp","wb") outfile.write(header) #below technique used encoding message def base10tobase2(number): little_endian_digits_list=[] power=0 while number>0: digit=number%(2**(power+1)) number=number-digit little_endian_digits_list.append(digit/(2**power)) power=power+1 while len(little_endian_digits_list)<8: little_endian_digits_list.append(0) return little_endian_digits_list y=54 x in messagecontent: base2list=base10tobase2(ord(x)) z in base2list: if ord(body[y])==255: z=z*(-1) outfile.write(chr(ord(body[y])+z)) y=y+1 outfile.write(body[54:len(body)]) # below want use encoded bitmap , original bitmap obtain message infile=open("c:\users\livio\desktop\imo2015.bmp","rb") body=infile.read() outfile=open("c:\users\livio\desktop\output.bmp","rb") body=outfile.read() message=open("c:\users\livio\desktop\mess.docx","wb") # below approach how obtain message in range(0, len(messagecontent)*8): list=[] t=0 w in body[54+i*8:54+(i+1)*8]: list.append(abs(ord(w)-ord(body[54+i*8+t]))) t=t+1 decimalnumber=sum(list[j]*(2**j) j in range(0, 8)) message.write(chr(decimalnumber)) # when surpass 255 , why? message.write(body[54:54+len(messagecontent)*8]) traceback (most recent call last): file "c:\users\livio\desktop\pro.py", line 42, in <module> message.write(chr(decimal)) valueerror: chr() arg not in range(256)
how can resolve problem? have tried far see when goes beyond 255, haven't succeeded. suggest?
also, please give other ideas if there way message in simpler way. maybe effort unnecessary before few lines of code.
the easiest way check when see goes beyond 255, putting print(decimalnumber)
right before message.write(chr(decimalnumber))
line.
for general design considerations, suggest not using magic numbers (what 8, or 2, or 255 in context of code? give constants names: e.g. ascii_range = 255
)
i suggest dividing code series of smaller steps functions, , run , test each function separately. check out unit testing suite python.
also checkout unistr()
built-in if it's useful you're doing.
Comments
Post a Comment