0

I have a string '6273db00', and hexadecimal numbers in range '00000 - f423f'. I'm trying to append each value from that range to the end of the string and print the result to a text file.

So it would look like this:

6273db0000000
6273db0000001
...
6273db00f423f

My code at the moment prints numbers starting with 1 digit:

6273db000
6273db001
6273db002

file = open("output.txt", "w")

string = "6273db00"

for i in range(0,1000000):
    result = string + format(i, 'x')
    file.write((result) + '\n')
file.close()    

What should I change to make it print 5 digit hex numbers?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Your range function is going to count from 0 to 100000. It won't generate hex alpha numeric strings like: f423f. – Captain Caveman May 05 '22 at 15:45
  • 1
    The largest hex string you'd get is for 99,999 - `"1869f"`. `"f423f"` is 999,999 - to make your intent more obvious you could write e.g. `range(0xf423f + 1)`. – jonrsharpe May 05 '22 at 15:46
  • @CaptainCaveman the last 3 results from the output file run with the code above: 6273db00f423d, 6273db00f423e, 6273db00f423f. I just need to make the progam add zeros If there's less than 5 digits in converted to hex value, so instead of '6273db00|1' I'd get '6273db00|00001' in the ouput. Anyway I can do this? – user193127 May 05 '22 at 16:01
  • Please don't put answers in the question. The dupe _does_ include how to zero-pad to a fixed number of digits (and _without_ separately `zfill`ing it). – jonrsharpe May 05 '22 at 16:50

0 Answers0