【发布时间】:2017-07-28 05:38:44
【问题描述】:
输入值后,我的代码有错误(见下面的代码)。我可以打包这些位,但拆包不起作用。有什么建议么?我不完全了解打包和拆包,文档有点混乱。
import struct
#binaryadder -
def binaryadder(input):
input = int(input)
d = struct.pack("<I", input)
print d
print type(d)
d = struct.unpack("<I",input)
print d
#Test Pack
count = 0
while True:
print "Enter input"
a = raw_input()
binaryadder(a)
count = count + 1
print "While Loop #%s finished\n" % count
在我输入字符串后,此代码会抛出以下错误:
Enter input
900
ä
<type 'str'>
Traceback (most recent call last):
File "C:\PythonPractice\Binarygenerator.py", line 25, in <module>
binaryadder(a)
File "C:\PythonPractice\Binarygenerator.py", line 17, in binaryadder
d = struct.unpack("<I",input)
struct.error: unpack requires a string argument of length 4
【问题讨论】:
标签: python struct python-2.x