如何在列表中下标二进制前导0b

时间:2016-02-24 16:11:46

标签: python binary subscript

所以我有二进制文字,但我需要删除每个文件中的前导'0b'。如何订阅它们呢?这是我目前的代码:

en = [132, 201, 141, 74, 140, 94, 141, 140, 141, 15, 31, 164, 90, 229, 201, 141, 78, 114, 241, 217, 141, 217, 140, 180, 141, 164, 51, 141, 188, 221, 31, 164, 241, 177, 141, 140, 51, 217, 141, 201, 229, 152, 141, 78, 241, 114, 78, 102, 94, 141, 74, 152, 31, 152, 141, 94, 201, 31, 164, 102, 164, 51, 90, 141, 201, 229, 164, 31, 201, 152, 152, 51, 115]

key = 84

#STEP 1 - 1ST XOR WITH KEY

for i in range(0, len(en)):
    en[i] = en[i] ^ key
    en[i] = bin(en[i])
    if len(en[i]) < 10:
        en[i] = '{:#010b}'.format(int(en[i],2))

print(en)
print(' ')

#STEP 2 - USE SBOX SUB ON EACH BLOCK NIBBLE
for i in range(0, len(en)):
    en[i] = list(en[i])

print(en)

1 个答案:

答案 0 :(得分:2)

只需从格式说明符中删除#字符,因为&#34; 表示整数,当使用二进制,八进制或十六进制输出时,此选项会添加相应的前缀&#39; 0b& #39;,&#39; 0o&#39;,或&#39; 0x&#39;输出值&#34; source)。例如:

In [3]: '{:08b}'.format(1)
Out[3]: '00000001'

顺便说一句,没有必要执行那么多次转换。你可以缩短第一个循环:

for i in range(len(en)):
    en[i] = '{:08b}'.format(en[i] ^ key)