Binary to ASCII in Python -
i'm trying decode binary located in .txt file, i'm stuck. don't see possibilities can go around.
def code(): testestest ascii = {'01000001':'a', ...} binary = {'a':'01000001', ...} print (ascii, binary) def encode(): pass def decode(code,n): f = open(code, mode='rb') # open file filename <code> while true: chunk = f.read(n) # read n characters @ time open file if chunk == '': # 1 way check end of file in python break if chunk != '\n': # process it???? pass
how can take binary in .txt file , output ascii?
from example, input looks string of binary formatted number.
if so, don't need dictionnary that:
def byte_to_char(input): return chr(int(input, base=2))
using data gave in comments, have split binary string bytes.
input ='01010100011010000110100101110011001000000110100101110011001000000110101001110101011100110111010000100000011000010010000001110100011001010111001101110100001000000011000100110000001110100011000100110000' length = 8 input_l = [input[i:i+length] in range(0,len(input),length)]
and then, per byte, convert char:
input_c = [chr(int(c,base=2)) c in input_l] print ''.join(input_c)
putting together:
def string_decode(input, length=8): input_l = [input[i:i+length] in range(0,len(input),length)] return ''.join([chr(int(c,base=2)) c in input_l]) decode(input) >'this test 10:10'
Comments
Post a Comment