python读取一个字节后按位打印出来

python读取一个字节后按位打印出来,第1张

python读取一个字节后按位打印出来
#按位打印出来
f = open("E:\data211008151402.dat", 'rb')
# read one byte
byte = f.read(1)
# convert the byte to an integer representation
byte = ord(byte)
# now convert to string of 1s and 0s
s = hex(byte).zfill(2).encode()
byte = bin(byte)[2:].rjust(8, '0')
print(s)
# now byte contains a string with 0s and 1s
for bit in byte:
    print(bit)

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5157147.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-18
下一篇2022-11-18

发表评论

登录后才能评论

评论列表(0条)

    保存