1)我使用十六進制值操作時遇到了一些問題。爲什麼sendValue(hex(12289))
導致錯誤(中止腳本),而sendValue(0x3001)
的作品?使用十六進制值操作時不能使用十六進制功能
def sendValue(value):
for i in range(16):
if (value & 0x8000):
print "1" # later this bit will be sent to a LC
else:
print "0" # later this bit will be sent to a LC
value <<= 1 # corrected this
def main():
sendValue(0x3001)
sendValue(hex(12289))
if __name__ == '__main__':
main()
2)我所期望的輸出
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
1
但我只是得到0
**什麼**錯誤? – 2015-02-09 08:53:11
這是因爲'hex(12289)'返回一個'string',並且你將它與'if'中的'number'進行比較。所以它會拋出一個錯誤。 – 2015-02-09 08:58:58
你不要在循環中的任何地方使用'i'。你正在做'價值&0x8000' 16次。 – 2015-02-09 09:02:25