Using python to write hex to a file

I am trying to create a bunch of binaries containing the corresponding hex values

for i in range(2**8):
    file = open("test" + str(i) + ".bin", "wb")
    file.write(hex(i))
    file.close()

      

Unfortunately, it looks like the textual representation of my counter converted to hex is being written to files instead of the actual hex values. Can someone fix this code? I'm pretty sure the problem is withhex(i)

+3


source to share


1 answer


If you want the value to be written in binary, use chr () to create a character from i:



for i in range(2**8):
    with open("test" + str(i) + ".bin", "wb") as f:
        f.write(chr(i))

      

+4


source







All Articles