0

My text file have data as :

01100110011010010111001001110011011101000010000001101100011010010110111001100101 0111001101100101011000110110111101101110011001000010000001101100011010010110111001100101 01110100011010000110100101110010011001000010000001101100011010010110111001100101

so, I need to convert this data file to English by python. but my programme get some error as :

ValueError: invalid literal for int() with base 2: ''

Please help me to solve this

def bit2strings():
    with open('test_doc.txt', 'r' ) as f:
        x = (f.read())
        for line in x.split(' '):
            data = line
            if data =='':
                print(data)
                break
            else:
                data = f.read(8)
                plaintext = chr(int(data, 2))
                print(plaintext, end='')
                data = f.read(8)

1 Answer 1

1

I know that this is not more advance coding part. but how ever lastly write proper programme to solve my problem. I am a beginner to python. so then please give me some comment to do advance this coding part more than this. this is my coding part :

def bit2strings():
    with open('test_doc.txt', 'r') as f:
        for i in f:
            #print(i)
            for j in range(len(i)//8):
                s = ((i[j * 8:j * 8 + 8]))
                #print(s)            
                get_string = ''.join(chr(int(s, 2)))
                print(get_string, end='')
            print(end='\n')
        

if __name__=='__main__':
    bit2strings()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.