Check if a string is hexadecimal

后端 未结 11 1462
说谎
说谎 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:25

    In Python3, I tried:

    def is_hex(s):
        try:
            tmp=bytes.fromhex(hex_data).decode('utf-8')
            return ''.join([i for i in tmp if i.isprintable()])
        except ValueError:
            return ''
    

    It should be better than the way: int(x, 16)

提交回复
热议问题