0

I need a simple way to generate a list of hex numbers between certain start and finish value with fast performance using python.

For example:

If I enter 0 and FFF it would pad the output with zeroes to the largest number:

000
001
002
003
004
005
006
007
008
009
00A
00B
...
FFF

Any suggestions? I'm not a programmer so the best, simplest way to do this is may help me. Thanks

1 Answer 1

1

Using the built-in method range and format your output usng X(hex) format specifier:

>>> start = 0x000
>>> end = 0xFFF
>>> hex_list = ['0x{:03X}'.format(x) for x in range(int(start), int(end+1))]
['0x000', '0x001', '0x002', '0x003', '0x004', '0x005', '0x006', '0x007', '0x008', '0x009', 
'0x00A', '0x00B', '0x00C', '0x00D', '0x00E', '0x00F', '0x010', '0x011', '0x012', 
...
0xFEA', '0xFEB', '0xFEC', '0xFED', '0xFEE', '0xFEF', '0xFF0', '0xFF1', '0xFF2', '0xFF3', 
'0xFF4', '0xFF5', '0xFF6', '0xFF7', '0xFF8', '0xFF9', '0xFFA', '0xFFB', '0xFFC', '0xFFD', 
'0xFFE', '0xFFF']

Note that the output is in string, so if you want to read the hex. value, do:

>>> hex_value = int('0xFFF', base=16)
>>> hex_value
4095
Sign up to request clarification or add additional context in comments.

2 Comments

You can help me with full code where i want to print all outputs to a txt file. And coming to output we need each output in each line.
@neworld, have you tried anything by yourself?, I posted this to give you a starter, writing to file on each line shouldn't be that hard to do or find. see here: docs.python.org/3/tutorial/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.