首页 > 解决方案 > Text to binary converter doesn't work if I use numbers or special characters

问题描述

There's a problem to format the string as a binary number when a space or a special character is in the input.

phrase = "a b"
bin_str = '0'+'0'.join(format(ord(i), 'b') for i in phrase)
print(bin_str)
original_string = int('0b'+bin_str,2).to_bytes((int('0b'+bin_str,2).bit_length() + 7) // 8, 'big').decode()
print(original_string)

I should get:
011000010010000001100010
But instead I got:
01100001010000001100010
when I tried to decode, got the error message in the line 4:

"utf-8' codec can't decode byte 0xa0 in position 1: invalid start byte"

If the variable phrase is "año"
I should get:
01100001110000111011000101101111
But instead I got:
0110000101111000101101111
when I tried to decode, got the error message in the line 4:

"utf-8' codec can't decode byte 0xc2 in position 0: invalid continuation byte"

I know the codec error is happening because the binary number is wrong, but I don't know why the converter is working wrong when I use an space, special character or even a number, if the ord() method uses Unicode.

标签: pythontextbinaryconverters

解决方案


我改变了这个:bin_str = '0'+'0'.join(format(ord(i), 'b') for i in phrase)
到:bin_str = '0'+bin(int.from_bytes(phrase.encode(), 'big'))[2:].zfill(8)
现在完美!
PD:我仍然不知道为什么第一行不起作用的原因


推荐阅读