s = "中文"
b = bytes(s, encoding='utf-8')
print(b)
b1=[]
for i in range(len(b)):
print(b[i])
b1.append((b[i]^0xff))#
############
"""
python取反不能直接加~,因为整数如011,取反码位100,
实际的计算结果: ~4 = -5, ~-5 = 4
依据上述第四条,我们的解释思路是,确定原码===> 取反
(1) 求~4, 我们用八进制来表示4:
4的原码: 0000 0100
取反得到: 1111 1011, 观察符号,是负数,因为负数以补码存储的,所以问题转化为:
某个数x的补码是1111 1011,求x的值(由补码求原码)
取反: 0000 0100
+1: 0000 0101 = 5, 加上标点符号(负号) 得到结果: -5
"""
print(b1)
# print(new1)
with open ('1.txt','wb') as ds:
ds.write(bytes(b1))#转换字节
#########读文件
f= open('1.txt','rb')
txt1=f.read()
print(txt1)
b2=[]
for i in range(len(txt1)):
b2.append((txt1[i]^0xff))
print(b2)
print(bytes(b2))
#保存文件
with open ('2.txt','wb') as ds:
ds.write(bytes(b2))
# print(f.read())