簡體   English   中英

如何將二進制解碼為ASCII?

[英]How to decode binary to ASCII?

我是 python 程序員,我有一個問題,我可以將輸入轉換為二進制:

bnr = open("binary.bin", "w")
tobinary = input("Enter whatever you want : ")
limit = 100
d = tobinary.encode()
if limit >= len(tobinary):
    for i in d:
        bnr.write(bin(i)[2:])
    bnr.close()
bi = open("binary.bin", "r")
read = bi.read()
bi.close()
print(read)

但我不能做逆向請幫忙。

實際上,使用您正在執行的方法,您將無法執行此操作,因為所有二進制數據都已連接在一起。 相反,您需要在所有這些二進制文件之間放置一個分隔符,並使用 chr() 和 int() 重新創建數據。

bnr = open("binary.bin", "w")
tobinary = input("Enter whatever you want : ")
limit = 100
d = tobinary.encode()
if limit >= len(tobinary):
    for i in d:
        bnr.write(bin(i)[2:]+' ')
    bnr.close()
bi = open("binary.bin", "r")
read = bi.read()
bi.close();
print(''.join([chr(int(x,2)) for x in read.split(' ')[:-1]]))

我的最終代碼

#ASCII to Binary function
def ToBinary():
    bnr = open("binary.bin", "w")
    tobinary = input("Enter whatever you want : ")
    limit = 10000
    d = tobinary.encode()
    if limit >= len(tobinary):
        for i in d:
            bnr.write(bin(i)[2:]+' ')
        bnr.close()
    bi = open("binary.bin", "r")
    read = bi.read()
    bi.close();
    print(read)
#Binary to ASCII function
def ToASCII():
    aci = open("text.txt", "w")
    aci.write(''.join([chr(int(x,2)) for x in read.split(' ')[:-1]]))
    aci.close()
    asi = open("text.txt", "r")
    read1 = asi.read()
    asi.close()
    print(read1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM