Python Forum
checking for valid hexadecimal digits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
checking for valid hexadecimal digits
#1
i have a string that may be a valid AMI ID in AWS context. that would be "ami-" followed by 17 hexadecimal digits. checking the prefix and length is direct enough. i was hoping there was a str method like there is isdecimal() for decimal digits, but there isn't. so i am using my own function like:
def ishex(s):
    try:
        n = int(s,16)
        return True
    except ValueError:
        return False
i was wondering if there was something better in Python so i can ditch this little function.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Seems fine. Especially if you're already checking for length first, there won't really be any performance issues.

There is a minor edge case where this would return True for "0xA", while the "x" is not a hexadecimal digit.

If you had a huge string, then converting it into an int takes work that isn't necessary. In the large case, I'd probably use the re engine to look for non-hexadecimal digits. A string with one million hex digits takes a bit of time to go through int(), while the re.search test is very fast.

def ishex(s):
    return not re.search(r"[^A-F0-9]", s)
Could also do a set comparison which works quickly on large strings:

def ishex(s):
    return not set(s) - set("ABCDEF0123456789")
Reply
#3
a huge str of hexadecimal would be the absurd case. i can't envision it ever happening in realistic cases. these values will be user input and responses from AWS. a few hundred digits because someone was banging on the 'f' key to get it to quit bouncing and accidentally hit return is probably the worst case. a len() check should be good enough.

ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
extended discussion here https://stackoverflow.com/questions/1159...exadecimal
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Script getting reindexing only valid error cubangt 1 886 Dec-07-2023, 04:06 PM
Last Post: cubangt
Question Use function, retry until valid Ashcora 8 1,462 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  Single digits seem to be greater than double digits Frosty_GM 4 3,495 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
Smile send hexadecimal package variable UART santos20 2 2,177 Oct-30-2020, 11:40 AM
Last Post: Larz60+
  Limiting valid values for a variable in a class WJSwan 5 3,593 Jul-06-2020, 07:17 AM
Last Post: Gribouillis
  How to verify the give number is valid Mekala 3 2,393 May-16-2020, 02:40 PM
Last Post: anbu23
  C:\Windows\System32\appwiz.cpl is not a valid Win32 application Marceepoo 8 5,131 Mar-15-2020, 04:46 AM
Last Post: buran
  is a str object a valid iterator? Skaperen 6 5,621 Jan-27-2020, 08:44 PM
Last Post: Skaperen
  How to get valid error message while invoking REST API through Python gollupraveen 0 2,064 Oct-04-2019, 07:15 PM
Last Post: gollupraveen
  How to keep looping until the user input is valid ? KyawMyo 12 6,187 Jun-10-2019, 02:51 AM
Last Post: KyawMyo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020