0

I need to convert: 1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1110011 1110100 1110010 1101001 1101110 1100111 to string. I've converted string to binary

text2 = 'This is a string'
res = ' '.join(format(ord(i), 'b') for i in text2)

print(res)

#output:
1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1110011 1110100 1110010 1101001 1101110 1100111

And now I've problem to converting it back to string, chr gives me some asian characters:

c = ' '.join(chr(int(val)) for val in res.split(' '))

print(c)
#output:
󶦴 􌳈 􌳉 􎿻 𘚠 􌳉 􎿻 𘚠 􌣡 𘚠 􎿻 􏁔 􎿺 􌳉 􌴶 􌥏

Then I try binascii but also not working:

l = res.split()
print(l)
for i in l:
    print (binascii.b2a_uu(bytes(i, 'utf-8')))
    output:
    b"',3 Q,#$P,   \n"
    b"',3$P,3 P,   \n"
    b"',3$P,3 P,0  \n"
    b"',3$Q,# Q,0  \n"
    b'&,3 P,# P\n'
    b"',3$P,3 P,0  \n"
    b"',3$Q,# Q,0  \n"
    b'&,3 P,# P\n'
    b"',3$P,# P,0  \n"
    b'&,3 P,# P\n'
    b"',3$Q,# Q,0  \n"
    b"',3$Q,#$P,   \n"
    b"',3$Q,# Q,   \n"
    b"',3$P,3 P,0  \n"
    b"',3$P,3$Q,   \n"
    b"',3$P,#$Q,0  \n"
2
  • What do you mean by "also not working"? Commented Feb 4, 2021 at 2:58
  • @ScottHunter You can see the output, it's still not 'This is a string'. Commented Feb 4, 2021 at 3:00

1 Answer 1

1

You need to set the base for int:

''.join(chr(int(val, 2)) for val in res.split(' '))

Output:

'This is a string'
Sign up to request clarification or add additional context in comments.

1 Comment

What actually is this base "2"? I try to find more info to find out but can't find out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.