【发布时间】:2016-03-03 07:59:02
【问题描述】:
我正在尝试将二进制值转换为每个 1/0 的列表,但我得到的是默认二进制值而不是列表。
我有一个字符串,我将每个字符转换为二进制,它给了我一个列表,其中每个字符都有一个字符串。现在我试图将每个字符串拆分为值为 0/1 的整数,但我什么也得不到。
# if message = "CC"
message="CC"
# just a debug thing
for c in message:
asci = ord(c)
bin = int("{0:b}".format(asci))
print >> sys.stderr, "For %c, asci is %d and bin is %d" %(c,asci,bin)
c = ["{0:b}".format(ord(c)) for c in message]
# c = ['1000011', '1000011']
bin = [int(c) for c in c]
#bin should be [1,0,0,0,0,1,1,1,0,0,0,0,1,1]
# but I get [1000011, 1000011]
print >> sys.stderr, bin
【问题讨论】:
标签: python string list binary list-comprehension