Python - text file with all values 0 to 2^24 in HEX
<p>I am trying to create a file with all possible values for 0 up to 2^24 in HEX.</p>

<p>This is what I have so far:</p>

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

<p>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)?</p>

<p>Thank you</p>

<p>just use the format method from string when writting:</p>

<pre><code>writer.write("{:#X}".format(data_to_write))
</code></pre>

<p>Example:</p>

<pre><code>&gt;&gt;&gt; "{:#x}".format(123324)
'0x1e1bc'
&gt;&gt;&gt; "{:#X}".format(123324)
'0X1E1BC'
</code></pre>

python