如何在python中使用XOR遍历列表 [英] How to iterate through a list using XOR in python

查看:47
本文介绍了如何在python中使用XOR遍历列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我得到了以下代码:

telegram = "$00;02;A1;00000000*49";校验和 = 电报 [10:18] # 是 00000000对于电报中的 x[1:]:x = "{0:08b}".format(int(hex(ord(x)),16))打印 (x)

输出字符串telegram的每个字符的二进制值:

0011000000110000001110110011000000110010001110110100000100110001001110110011000000110000001100000011000000110000001100000011000000110000001010100011010000111001

现在我想获取电报的校验和,这意味着我必须使用按位运算符 ^.我确实得到了这样的正确结果:

#--snip--firstdigit = "{0:08b}".format(int(hex(ord(telegram[1])),16)) #telegram[1] = 0result_1 = int(firstdigit) ^ int(校验和)打印 (f'{result_1:08}') # 是 00110000seconddigit = "{0:08b}".format(int(hex(ord(telegram[2])),16)) #telegram[2] =0result_2 = int(result_1) ^ int(seconddigit)打印 (f'{result_2:08}') # 是 00000000第三位 = "{0:08b}".format(int(hex(ord(telegram[3])),16)) #telegram[3] =;result_3 = int(result_2) ^ int(thirddigit)打印 (f'{result_3:08}') # 是 00111011

...等等.(正确)输出:

001100000000000000111011

但这样做似乎很不方便,这让我想到了我的实际问题:我想遍历字符串 telegram 以获得所需的输出,而我只是无法掌握它.如果您能帮助我,我将不胜感激!

解决方案

您可以对每个字符使用函数 ord() 跳过到二进制字符串的转换.例如:

<预><代码>>>>电报 = $00;02;A1;00000000*49">>>ord(电报[1]) ^ ord(电报[2])0

您可以使用列表推导式将所有字符转换为序数:

<预><代码>>>>[ord(n) for n in telegram[1:]] # 除第一个字符外的所有字符...[48, 48, 59, 48, 50, 59, 65, 49, 59, 48, 48, 48, 48, 48, 48, 48, 48, 42, 52, 57]

使用标准库中的工具,例如 functools.reduceoperator.xor 您可以将所有值异或在一起:

<预><代码>>>>导入功能工具>>>进口经营者>>>functools.reduce(operator.xor,[ord(n) for n in telegram[1:]])110>>>format(110,'08b') # 二进制(如果需要)'01101110'

So I got the following code:

telegram = "$00;02;A1;00000000*49"
checksum = telegram[10:18]                           # is 00000000
for x in telegram[1:]:
    x = "{0:08b}".format(int(hex(ord(x)),16))
    print (x)

which puts out the binary value of each charakter of the string telegram:

00110000
00110000
00111011
00110000
00110010
00111011
01000001
00110001
00111011
00110000
00110000
00110000
00110000
00110000
00110000
00110000
00110000
00101010
00110100
00111001

Now I want to get the checksum of the telegram, meaning I have to use the bitwise operator ^. I do get the correct results like this:

#--snip--

firstdigit = "{0:08b}".format(int(hex(ord(telegram[1])),16))            # telegram[1] = 0
result_1 = int(firstdigit) ^ int(checksum)
print (f'{result_1:08}')                                          # is 00110000

seconddigit = "{0:08b}".format(int(hex(ord(telegram[2])),16))           # telegram[2] =0
result_2 = int(result_1) ^ int(seconddigit)
print (f'{result_2:08}')                                          # is 00000000                    

thirddigit = "{0:08b}".format(int(hex(ord(telegram[3])),16))            # telegram[3] =;
result_3 = int(result_2) ^ int(thirddigit)
print (f'{result_3:08}')                                          # is 00111011  

...and so on. (Correct) Output:

00110000
00000000
00111011

But doing that seems really inconvenient, which brings me to my actual problem: I want to loop through the string telegram in order to get the desired outputs and I just can't get a hang of it. Would really appreciate it if you could help me out!

解决方案

You can skip the conversion to a binary string using the function ord() on each character. For example:

>>> telegram = "$00;02;A1;00000000*49"
>>> ord(telegram[1]) ^ ord(telegram[2])
0

You can convert all the characters to ordinals with a list comprehension:

>>> [ord(n) for n in telegram[1:]] # all but first character...
[48, 48, 59, 48, 50, 59, 65, 49, 59, 48, 48, 48, 48, 48, 48, 48, 48, 42, 52, 57]

With tools in the standard library like functools.reduce and operator.xor you can XOR all the values together:

>>> import functools
>>> import operator
>>> functools.reduce(operator.xor,[ord(n) for n in telegram[1:]])
110
>>> format(110,'08b') # binary if needed
'01101110'

这篇关于如何在python中使用XOR遍历列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆