Check if a string is hexadecimal

后端 未结 11 1485
说谎
说谎 2020-12-13 06:14

I know the easiest way is using a regular expression, but I wonder if there are other ways to do this check.

Why do I need this? I am writing a Python script that re

11条回答
  •  佛祖请我去吃肉
    2020-12-13 06:19

    Simple solution in case you need a pattern to validate prefixed hex or binary along with decimal

    \b(0x[\da-fA-F]+|[\d]+|0b[01]+)\b
    

    Sample: https://regex101.com/r/cN4yW7/14

    Then doing int('0x00480065006C006C006F00200077006F0072006C00640021', 0) in python gives 6896377547970387516320582441726837832153446723333914657

    The base 0 invokes prefix guessing behaviour. This has saved me a lot of hassle. Hope it helps!

提交回复
热议问题