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"
asked Feb 4, 2021 at 2:56
Ziggy Witkowski
812 silver badges8 bronze badges
-
What do you mean by "also not working"?Scott Hunter– Scott Hunter2021εΉ΄02ζ04ζ₯ 02:58:10 +00:00Commented Feb 4, 2021 at 2:58
-
@ScottHunter You can see the output, it's still not 'This is a string'.Ziggy Witkowski– Ziggy Witkowski2021εΉ΄02ζ04ζ₯ 03:00:22 +00:00Commented Feb 4, 2021 at 3:00
1 Answer 1
You need to set the base for int:
''.join(chr(int(val, 2)) for val in res.split(' '))
Output:
'This is a string'
answered Feb 4, 2021 at 3:28
Chris
29.8k3 gold badges34 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Ziggy Witkowski
What actually is this base "2"? I try to find more info to find out but can't find out.
lang-py