Python中int、str、bytes相互转化,以及转换后2进制、16进制显示

源类型目标类型方式结果
intstrstr(10)、‘{0}’.format(10)10 => ‘10’
intstr(16进制)hex(10)10 => ‘0xa’
intstr(2进制)bin(10).replace(‘0b’,‘’)10 => ‘1010’
strintint(‘10’)‘10’ => 10
str(16进制)intint(‘0xa’, 16)‘0xa’ => 10
str(2进制)intint(‘1010’, 2)‘1010’ => 10
intbytesnum.to_bytes(length=4, byteorder=‘little’, signed=False)4665 => b’\x39\x12\x00\x00’
intbytesstruct.pack(“<I”, 4665)4665 => b’\x39\x12\x00\x00’
bytesintint.from_bytes(b’\x39\x12\x00\x00’, byteorder=‘little’, signed=False)b’\x39\x12\x00\x00’ => 4665
bytesintstruct.unpack(“<I”, b’\x39\x12\x00\x00’)b’\x39\x12\x00\x00’ => 4665
int[]bytesbytes([57, 18, 0, 0])[57, 18, 0, 0] => b’\x39\x12\x00\x00’
bytesint[][x for x in b’9\x12\x00\x00’]b’9\x12\x00\x00’ => [57, 18, 0, 0]
strbytes‘美好’.encode(‘utf-8’)‘美好’ => b’\xe7\xbe\x8e\xe5\xa5\xbd’
strbytesbytes(‘美好’, ‘utf-8’)‘美好’ => b’\xe7\xbe\x8e\xe5\xa5\xbd’
bytesstrb’\xe7\xbe\x8e\xe5\xa5\xbd’.decode(‘utf-8’)b’\xe7\xbe\x8e\xe5\xa5\xbd’ => ‘美好’
bytesbytes(无\x)binascii.b2a_hex(b’\xe7\xbe\x8eqaq’)b’\xe7\xbe\x8eqaq’ => b’e7be8e716171’
bytesbytes(有\x)binascii.a2b_hex(b’e7be8e716171’)b’e7be8e716171’ => b’\xe7\xbe\x8eqaq’
bytesstr(hex)b’\xe7\xbe\x8eqaq’.hex()b’\xe7\xbe\x8eqaq’ => ‘e7be8e716171’
str(hex)bytesbytes.fromhex(‘e7be8e716171’)‘e7be8e716171’ => b’\xe7\xbe\x8eqaq’
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值