【问题标题】:How to keep leading zeros in binary integer (python)?如何在二进制整数(python)中保持前导零?
【发布时间】:2014-05-09 11:31:02
【问题描述】:

我需要使用 XOR 计算十六进制串行字串的校验和。据我(有限的)知识,这必须使用按位运算符 ^ 来执行。此外,数据必须转换为二进制整数形式。下面是我的基本代码 - 但它计算的校验和是 1000831。它应该是 01001110 或 47hex。我认为错误可能是由于缺少前导零。我尝试添加前导零的所有格式都将二进制整数转换回字符串。我很感激任何建议。

    word = ('010900004f')

    #divide word into 5 separate bytes
    wd1 = word[0:2] 
    wd2 = word[2:4]
    wd3 = word[4:6]
    wd4 = word[6:8]
    wd5 = word[8:10]

    #this converts a hex string to a binary string
    wd1bs = bin(int(wd1, 16))[2:] 
    wd2bs = bin(int(wd2, 16))[2:]
    wd3bs = bin(int(wd3, 16))[2:]
    wd4bs = bin(int(wd4, 16))[2:]

    #this converts binary string to binary integer
    wd1i = int(wd1bs)
    wd2i = int(wd2bs)
    wd3i = int(wd3bs)
    wd4i = int(wd4bs)
    wd5i = int(wd5bs)

    #now that I have binary integers, I can use the XOR bitwise operator to cal cksum
    checksum = (wd1i ^ wd2i ^ wd3i ^ wd4i ^ wd5i)

    #I should get 47 hex as the checksum
    print (checksum, type(checksum))

【问题讨论】:

  • 我认为这在see this question之前已经解决了
  • 0x47 != 0b1001110。很少有奇数在其二进制表示中以0 结尾。
  • @PyNEwbie 确实如此,但在这里我们面临着一个卓越的 XY 问题。
  • 什么是“二进制整数”?您为什么将基数为 2 的数字字符串解释为基数为 10 的数字字符串?我认为你把数字和它们的表示搞混了,这就是为什么你要走这条二进制弯路。
  • @user3284986 我总是觉得区分数字的“表示”和它的“值”是很实用的。 0x2a、0b101010 和 42 都具有相同的值。但是值 42 可以表示为0x2a、0b101010 或42。整数不是二进制,也不是十进制,也不是十六进制、三进制、一元或格雷码:整数是整数,即 Z 的元素。它的表示可以是二进制、十进制等,pp。

标签: python python-2.7


【解决方案1】:

为什么要使用所有这些转换和昂贵的字符串函数?

(我会回答你XY-Problem 的 X 部分,而不是 Y 部分。)

def checksum (s):
    v = int (s, 16)
    checksum = 0
    while v:
        checksum ^= v & 0xff
        v >>= 8
    return checksum

cs = checksum ('010900004f')
print (cs, bin (cs), hex (cs) )

结果如预期的那样是 0x47。顺便说一句,0x47 是 0b1000111 而不是 0b1001110。

【讨论】:

  • 谢谢 - 这很有说服力。它确实解决了 X 部分,使 Y 部分变得多余。多余却神秘。 . .
  • @user3284986 很高兴为您提供帮助。没那么神秘,只需查看 PyNewbie 在他对您的问题的评论中发布的链接。
  • @Victory 我不会称之为技巧,而是一种标准方法。
【解决方案2】:
s = '010900004f'
b = int(s, 16)
print reduce(lambda x, y: x ^ y, ((b>> 8*i)&0xff for i in range(0, len(s)/2)), 0)

【讨论】:

  • 将在 python3 上失败,我怀疑 OP 正在使用它,因为他使用 print 作为函数。
  • @Hyperboreus:可能,但它被标记为 2.7。
  • @DSM 我可以发誓,当我写那条评论时,它没有被标记为任何特定版本。我一定是瞎了。
【解决方案3】:

就这样修改。

之前:

wd1i = int(wd1bs)
wd2i = int(wd2bs)
wd3i = int(wd3bs)
wd4i = int(wd4bs)
wd5i = int(wd5bs)

之后:

wd1i = int(wd1bs, 2)
wd2i = int(wd2bs, 2)
wd3i = int(wd3bs, 2)
wd4i = int(wd4bs, 2)
wd5i = int(wd5bs, 2)

为什么您的代码不起作用?

因为您误解了int(wd1bs) 的行为。 请参阅文档here。所以 Python int 函数 expect wd1bs 默认是 10 基数。 但是您希望 int 函数将其参数视为 2 基数。 所以你需要写成int(wd1bs, 2)


或者你也可以像这样重写你的整个代码。所以在这种情况下你不需要使用bin 函数。这段代码与@Hyperboreus 的答案基本相同。 :)

w = int('010900004f', 16)
w1 = (0xff00000000 & w) >> 4*8
w2 = (0x00ff000000 & w) >> 3*8
w3 = (0x0000ff0000 & w) >> 2*8
w4 = (0x000000ff00 & w) >> 1*8
w5 = (0x00000000ff & w)

checksum = w1 ^ w2 ^ w3 ^ w4 ^ w5

print hex(checksum)
#'0x47'

而且这个更短。

import binascii
word = '010900004f'
print hex(reduce(lambda a, b: a ^ b, (ord(i) for i in binascii.unhexlify(word))))
#0x47

【讨论】:

  • 你所有的行wx = (0x....可以写成wX = (w >> Y*8) & 0xff。只需先 shift 后掩码,则始终为 0xff。
  • @Hyperboreus:啊,这是更聪明的方式,谢谢。我的代码总是很冗长... :)
  • @user3284986:我添加了解释看看你的代码有什么问题。
  • @user2931409 - 非常感谢你。您对我对以 10 为底的默认值的错误假设感到满意。如果它有助于可读性,我不介意冗长 - 这是像我这样的 python 新手需要的。 . . .但最终,我们都希望尽可能少地打字:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-09
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
相关资源
最近更新 更多