Using python to write hex to file

encoding, file, hex, python

Solution

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

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

Problem

I am trying to create a bunch of binary files that contain corresponding hex values ``` for i in range(2**8): file = open("test" + str(i) + ".bin", "wb") file.write(hex(i)) file.close() ``` Unfortunately it appears that a text representation of my counter converted to hex is being written to the files instead of the actual hex values. Can someone please correct this code? I'm sure the problem is with `hex(i)`

Original source