3

I want to make a range in python from variable a and variable b

a = 0x88
b = 0xff
for x range(a, b):
    print(x)

from my code the result like this 136, 137, 138, 139 ...

but I want the result of looping like this

88, 89, 8a, 8b, 8c, 8d, 8e, 8f, 90, 91 .... ff

please help me, thank you

Zmx Atah
  • 85
  • 1
  • 7
  • https://stackoverflow.com/questions/16212965/incrementing-iterating-between-two-hex-values-in-python does this answer your question? – kluvin Nov 26 '20 at 19:23
  • Does this answer your question? [Incrementing (iterating) between two hex values in Python](https://stackoverflow.com/questions/16212965/incrementing-iterating-between-two-hex-values-in-python) – SiHa Nov 26 '20 at 19:23
  • 1
    `print(f"{x:x}")` where the 2nd `x` is the formatting to use. –  Nov 26 '20 at 19:26
  • You want the result to be the _strings_ `"88"`, `"89"` and etc...? Or a single string `"88, 89, 8a, ..."`? Is this a question about how to display the numbers or the numbers themselves? – tdelaney Nov 26 '20 at 19:29

3 Answers3

3

You can use hex(x) to convert x to a hexadecimal number.

Therefore your code will look like this:

a = 0x88
b = 0xff
for x in range(a, b):
    print(hex(x))

user14678216
  • 2,886
  • 2
  • 16
  • 37
3

Use hex() function:

a = 0x88
b = 0xff
for x in range(a, b):
    print(hex(x))

results in:

0x88
0x89
0x8a
0x8b
0x8c
0x8d
0x8e
0x8f
0x90
0x91
0x92
0x93
0x94
...
Hamza
  • 5,373
  • 3
  • 28
  • 43
1

Formatting as you asked:

a = 0x00
b = 0x100
for x in range(a, b):
    print('{:02x}'.format(x))

will generate "88" through "ff"

DrStrangepork
  • 2,955
  • 2
  • 27
  • 33