I was trying to build an encryption program in python 2.7. It would read the binary from a file and then use a key to encrypt it. However, I quickly ran into a problem. Files like image files and executables read as hex values. However, text files do not using open(). Even if i run
file=open("myfile.txt", "rb")
out=file.read()
it still comes out as just text. I'm on windows 7, not linux which i think may make a difference. Is there any way i could read the binary from ANY file (including text files), not just image and executable files?
解决方案
Even when reading a file with the 'rb' flag,
if your file has the byte '\x41' it will be printed as the letter 'A' in the console.
If you want the hex values, encode the file content as hex, which means:
content = open('text.txt', 'rb').read()
hex = content.encode('hex')