我如何在python中验证以下以下输入(十六进制空格值)

时间:2019-01-08 23:43:38

标签: python hex

我有一个以ascii编码的,以十六进制间隔的字符串

a="%27+%2B+%27%27+%2B+%2701 01 01 02 04 12
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00'
"

我需要验证,可以检查其是否包含有效字符

我随附的解决方案是

def Check():
    while False:
        for char in Hex:
            if char not in Valid:
                print ('That is an invalid hex value.')
        print('That is a valid hex value.')
        return Check

Hex=int(input('Enter a hex number: '), 16)
Valid='1''2''3''4''5''6''7''8''9''10''A''B''C''D''E''F'

1 个答案:

答案 0 :(得分:0)

只需:

Hex=input('Enter a hex number: ')
Valid='12345678910ABCDEF'
d={True:'That is a valid hex value.', False:'That is an invalid hex value.'}
print(d[all(i in Valid for i in str(Hex))])

示例输出:

Enter a hex number: 0123456789
That is a valid hex value.

Enter a hex number: $%*@345572
That is an invalid hex value.