-2

I am trying to create a file with all possible values for 0 up to 2^24 in HEX.

This is what I have so far:

file_name = "values.txt"
counter = 0
value = 0x0000000000
with open (file_name, 'w') as writer:
    while counter < 16777216:
        data_to_write = str(value) + '\n' 
        writer.write(data_to_write)
        counter = counter + 1
        value = value + 0x0000000001

This do what I want but with integers. Is there an easy way to turn this to HEX values in file instead (still as string)?

Thank you

Matrilx
  • 3
  • 3

2 Answers2

0

just use the format method from string when writting:

writer.write("{:#X}".format(data_to_write))

Example:

>>> "{:#x}".format(123324)
'0x1e1bc'
>>> "{:#X}".format(123324)
'0X1E1BC'
Netwave
  • 40,134
  • 6
  • 50
  • 93
0

Thank you for your help.

It is working now:

file_name = "value.txt"
counter = 0
value = 0x0000000000
with open (file_name, 'w') as writer:
    while counter < 10000:
        data_to_write = str(hex(value)[2:].zfill(6)) + '\n' 
        writer.write(data_to_write)
        counter = counter + 1
        value = value + 0x0000000001
Matrilx
  • 3
  • 3