Python:需要帮助拆分二进制代码的输入,不带空格
需要每 8 个字符拆分一次,这样它就会成为一个列表,然后我可以将其翻译成 ascii,然后翻译成英语。我只是不知道如何将输入(一大串二进制数字)拆分为可读的二进制数字,而不仅仅是一个字符串。
例如,输入字符串“010000010100001001000011”可以拆分为八位字节,如下所示:“01000001”、“01000010”、“01000011”。
到目前为止我所拥有的:
def main():
import string
#take user input of binary
code = raw_input ('Please type in your binary code to be decoded: ')
#split the code
for word in code:
print code[0::8] + ' '
#replace the input with the variables
ascii = ' '
for word in code:
ascii = ascii + int(word,2)
english = ' '
for word in acsii:
english = english + chr(word)
#print the variables to the user
print english
#call Main
main()
need to split every 8 char so it will become a list whereby i can then translate into ascii and then english. I just am at a loss for how to split the input,(one large string of binary numbers) into readable binary numbers, instead of just one string.
For example, the input string "010000010100001001000011" can be split in to octets as follows: "01000001","01000010","01000011".
What i have so far:
def main():
import string
#take user input of binary
code = raw_input ('Please type in your binary code to be decoded: ')
#split the code
for word in code:
print code[0::8] + ' '
#replace the input with the variables
ascii = ' '
for word in code:
ascii = ascii + int(word,2)
english = ' '
for word in acsii:
english = english + chr(word)
#print the variables to the user
print english
#call Main
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该可以在某种程度上帮助您使用列表理解:
This should help you some of the way, with list comprehensions:
我不确定您是否想要这个,也许值得:
但如果您只想要“01”格式:
谢谢 Ignacio,我必须睡觉了。
I'm not sure that you want this, you maybe it worth it:
But if you just want the '01' format:
thanks Ignacio, i must sleep.
我不确定我是否理解你的问题,但似乎你正在尝试执行以下操作:给定一个长度为 8n 的字符串,将每个 8 个二进制数字块转换为一个(unicode)字符串,然后加入结果字符串不带空格。
如果是这种情况,那么这将解决问题:
编辑:
您提到“我希望它们采用 ASCII,然后采用英文字母”,然后执行以下操作:
但要小心,chr 仅在范围(256)中有效。
I am not sure I understand your question, but it seems that you are trying to do the following: Given an string of length
8n
, convert each chunk of 8 binary digits into a (unicode) string then join the resulting string with no spaces.If this is the case, then this will do the trick:
Edit:
You mention "I want them in ASCII and then in English letters", then do the following:
but be careful, chr is only valid in range(256).