| 源类型 | 目标类型 | 方式 | 结果 |
|---|---|---|---|
| int | str | str(10)、‘{0}’.format(10) | 10 => ‘10’ |
| int | str(16进制) | hex(10) | 10 => ‘0xa’ |
| int | str(2进制) | bin(10).replace(‘0b’,‘’) | 10 => ‘1010’ |
| str | int | int(‘10’) | ‘10’ => 10 |
| str(16进制) | int | int(‘0xa’, 16) | ‘0xa’ => 10 |
| str(2进制) | int | int(‘1010’, 2) | ‘1010’ => 10 |
| int | bytes | num.to_bytes(length=4, byteorder=‘little’, signed=False) | 4665 => b’\x39\x12\x00\x00’ |
| int | bytes | struct.pack(“<I”, 4665) | 4665 => b’\x39\x12\x00\x00’ |
| bytes | int | int.from_bytes(b’\x39\x12\x00\x00’, byteorder=‘little’, signed=False) | b’\x39\x12\x00\x00’ => 4665 |
| bytes | int | struct.unpack(“<I”, b’\x39\x12\x00\x00’) | b’\x39\x12\x00\x00’ => 4665 |
| int[] | bytes | bytes([57, 18, 0, 0]) | [57, 18, 0, 0] => b’\x39\x12\x00\x00’ |
| bytes | int[] | [x for x in b’9\x12\x00\x00’] | b’9\x12\x00\x00’ => [57, 18, 0, 0] |
| str | bytes | ‘美好’.encode(‘utf-8’) | ‘美好’ => b’\xe7\xbe\x8e\xe5\xa5\xbd’ |
| str | bytes | bytes(‘美好’, ‘utf-8’) | ‘美好’ => b’\xe7\xbe\x8e\xe5\xa5\xbd’ |
| bytes | str | b’\xe7\xbe\x8e\xe5\xa5\xbd’.decode(‘utf-8’) | b’\xe7\xbe\x8e\xe5\xa5\xbd’ => ‘美好’ |
| bytes | bytes(无\x) | binascii.b2a_hex(b’\xe7\xbe\x8eqaq’) | b’\xe7\xbe\x8eqaq’ => b’e7be8e716171’ |
| bytes | bytes(有\x) | binascii.a2b_hex(b’e7be8e716171’) | b’e7be8e716171’ => b’\xe7\xbe\x8eqaq’ |
| bytes | str(hex) | b’\xe7\xbe\x8eqaq’.hex() | b’\xe7\xbe\x8eqaq’ => ‘e7be8e716171’ |
| str(hex) | bytes | bytes.fromhex(‘e7be8e716171’) | ‘e7be8e716171’ => b’\xe7\xbe\x8eqaq’ |
Python中int、str、bytes相互转化,以及转换后2进制、16进制显示
于 2024-07-09 15:54:46 首次发布

1577

被折叠的 条评论
为什么被折叠?



