python - 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.
解决方案
我改变了这个: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:我仍然不知道为什么第一行不起作用的原因
推荐阅读
- markdown - 如何从 FrontMatter 创建列表 - Hugo
- java - 限制调度程序仅在一个实例中运行
- c++ - 在 wxWidgets 中调整无边框 wxFrame 的大小
- kubernetes - 节点导出器 node_sockstat_TCP_alloc 与 node_netstat_Tcp_CurrEstab 不一致
- git - 为什么 VScode 不断打开我的用户文件夹作为存储库?
- maven - Mapstruct - 找不到符号 [Kotlin + Maven]
- javascript - 在动态创建的元素上按类 AddEventListener
- html - 在私有 IP 地址上部署内容?
- reactjs - 使用 S3 和 CloudFront 反应 App url 参数
- excel - 使用联合函数组合范围的错误消息