Search code examples
pythoncomparehexstring-comparison

How to check which hexadecimal numbers are within a range expressed by hexadecimal numbers?


I have a list of hexadecimal numbers and I want to find what of thi number are within a specific range, without the use of range python function.

The list of hexadecimal number is the following:

h = ["00000100", "000000c0", "000000a0" "00000620", "00000660", "00006000"]

I want to find numbers which are greater than "00000100" and less than "00000610" in python.

So how can I do it?

I have tried with the following code:

h = ["00000100", "000000c0", "000000a0" "00000620", "00000660", "00006000"]
num = 
for i in h:
    if i >="00000100"and <= "00000610":
        print(i)

Solution

  • The int built-in function can convert a string in any base:

    h = ["00000100", "000000c0", "000000a0" "00000620", "00000660", "00006000"]
    
    for s in h:
        if int("00000100", 16) <= int(s, 16) <= int("00000610", 16):
            print(s)
    

    Prints:

    00000100