Python:需要帮助拆分二进制代码的输入,不带空格

发布于 2025-01-04 21:00:25 字数 721 浏览 0 评论 0原文

需要每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱,才寂寞 2025-01-11 21:00:25

这应该可以在某种程度上帮助您使用列表理解:

>>> b = '010000010100001001000011'
>>> bin_chunks = [b[8*i:8*(i+1)] for i in xrange(len(b)//8)]
>>> print bin_chunks
['01000001', '01000010', '01000011']
>>> ints = [int(x, 2) for x in bin_chunks]
>>> print ints
[65, 66, 67]
>>> chars = [chr(x) for x in ints]
>>> print chars
['A', 'B', 'C']
>>> print ''.join(chars)
ABC

This should help you some of the way, with list comprehensions:

>>> b = '010000010100001001000011'
>>> bin_chunks = [b[8*i:8*(i+1)] for i in xrange(len(b)//8)]
>>> print bin_chunks
['01000001', '01000010', '01000011']
>>> ints = [int(x, 2) for x in bin_chunks]
>>> print ints
[65, 66, 67]
>>> chars = [chr(x) for x in ints]
>>> print chars
['A', 'B', 'C']
>>> print ''.join(chars)
ABC
诗化ㄋ丶相逢 2025-01-11 21:00:25
>>> re.findall('[01]{8}', '010000010100001001000011')
['01000001', '01000010', '01000011']
>>> ''.join(chr(int(x, 2)) for x in re.findall('[01]{8}', '010000010100001001000011'))
'ABC'
>>> re.findall('[01]{8}', '010000010100001001000011')
['01000001', '01000010', '01000011']
>>> ''.join(chr(int(x, 2)) for x in re.findall('[01]{8}', '010000010100001001000011'))
'ABC'
蹲墙角沉默 2025-01-11 21:00:25

我不确定您是否想要这个,也许值得:

>>> s = "010000010100001001000011"
>>> [int(s[i:i+8], 2) for i in xrange(0, len(s), 8)]
[65, 66, 67]

但如果您只想要“01”格式:

>>> s = "010000010100001001000011"
>>> [s[i:i+8] for i in xrange(0, len(s), 8)]
['01000001', '01000010', '01000011']

谢谢 Ignacio,我必须睡觉了。

I'm not sure that you want this, you maybe it worth it:

>>> s = "010000010100001001000011"
>>> [int(s[i:i+8], 2) for i in xrange(0, len(s), 8)]
[65, 66, 67]

But if you just want the '01' format:

>>> s = "010000010100001001000011"
>>> [s[i:i+8] for i in xrange(0, len(s), 8)]
['01000001', '01000010', '01000011']

thanks Ignacio, i must sleep.

清眉祭 2025-01-11 21:00:25

我不确定我是否理解你的问题,但似乎你正在尝试执行以下操作:给定一个长度为 8n 的字符串,将每个 8 个二进制数字块转换为一个(unicode)字符串,然后加入结果字符串不带空格。

如果是这种情况,那么这将解决问题:

stream = "010000010100001001000011"
grouped = [stream[n:n+8] for n in range(len(stream)/8)]
characters = [unichr(int(c, 2)) for c in grouped]
result = u"".join(characters)
# returns u'A\x82\x05'

编辑:
您提到“我希望它们采用 ASCII,然后采用英文字母”,然后执行以下操作:

ascii = [int(c, 2) for c in grouped] # this is a list of decimal ascii codes
english = [char(a) for a in ascii] # this is a list of characters - NOT UNICODE

但要小心,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:

stream = "010000010100001001000011"
grouped = [stream[n:n+8] for n in range(len(stream)/8)]
characters = [unichr(int(c, 2)) for c in grouped]
result = u"".join(characters)
# returns u'A\x82\x05'

Edit:
You mention "I want them in ASCII and then in English letters", then do the following:

ascii = [int(c, 2) for c in grouped] # this is a list of decimal ascii codes
english = [char(a) for a in ascii] # this is a list of characters - NOT UNICODE

but be careful, chr is only valid in range(256).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文