1 name = "吴彦祖" 2 for i in name: 3 i_by = bytes(i, encoding = "utf-8") 4 for i_bin in i_by: 5 i_b = bin(i_bin) 6 print(i_b)
输出结果:
0b10110100 0b10100110 0b10010110
我们来详细解读每个转换步骤:
for i in name: 通过for循环获取所有的字符i,共获取了3个字符
i_by = bytes(i, encoding = "utf-8") 我们通过bytes(“字符串”,编码格式)将所有字符i通过utf-8编码格式转换成字节i_by,共获取了9个字节
for i_bin in i_by: 通过for循环获取所有字节i_by以10进制显示为i_bin
i_b = bin(i_bin) 通过bin()转换成二进制
print(i_b)输出二进制显示
时间: 2024-10-10 04:25:14