Zmx Atah
Zmx Atah

Reputation: 95

how to make range in hexadecimal python?

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

Upvotes: 4

Views: 6298

Answers (3)

DrStrangepork
DrStrangepork

Reputation: 3134

Formatting as you asked:

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

will generate "88" through "ff"

Upvotes: 2

Hamza
Hamza

Reputation: 6025

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
...

Upvotes: 5

user14678216
user14678216

Reputation: 3373

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))

Upvotes: 4

Related Questions