I am trying to read a bmp file. I get diferent notations on the bytes I read. I would like to understand why the "read" method acts this way.
For that purpose: - I open the file in binary read mode. - I create an empty list. - I go through the binary file reading it byte by byte. - Each time I read a byte I load it in the list.
Why does it acts sometimes in a way and others in a different way? Why doesn't it always return an hex?
def main():
fichero = open("C:\\Users\\gsanmar\\Pictures\\astilleros-ferrol.bmp", "rb")
bytesDelFichero = []
for i in range(0,70):
bytesDelFichero.append(fichero.read(1))
for valor in bytesDelFichero:
print(valor)
main()
Actual results are:
b'B' b'M' b'\xf6' b'i' b'0' b'\x00' b'\x00' b'\x00' b'\x00' b'\x00' b'6' b'\x00' b'\x00' b'\x00' b'(' b'\x00' b'\x00' b'\x00' b'@' b'\x06' b'\x00' b'\x00' b'\x95' b'\x02' ...
That's how bytes are printed - if they're printable characters (letters, numbers...), you will see them in output instead of hex codes.
If you want hex codes everywhere, do something like:
for valor in bytesDelFichero:
print(hex(int(valor)))