Pythonint与byte类型相互转化
1根据Python⾃定义的功能,使⽤to_bytes函数转化int类型数据为byte型,然后使⽤from_bytes将byte类型数据转化为int型。
2def to_bytes(lf, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__计算机显示器
3 """
假座4 _bytes(length, byteorder, *, signed=Fal) -> bytes
5
6 Return an array of bytes reprenting an integer.
7
8 The integer is reprented using length bytes. An OverflowError is
9 raid if the integer is not reprentable with the given number of
10 bytes.
11
12 The byteorder argument determines the byte order ud to reprent the
13 integer. If byteorder is 'big', the most significant byte is at the
14 beginning of the byte array. If byteorder is 'little', the most
15 significant byte is at the end of the byte array. To request the native
绝对值的代数意义>一支铅笔的梦想16 byte order of the host system, u `sys.byteorder' as the byte order value.
17
18 The signed keyword-only argument determines whether two's complement is
19 ud to reprent the integer. If signed is Fal and a negative integer
20 is given, an OverflowError is raid.
21 """
做梦梦到拉屎22 pass
1def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
2 """
3 int.from_bytes(bytes, byteorder, *, signed=Fal) -> int
4
5 Return the integer reprented by the given array of bytes.等闲的意思
6
7 The bytes argument must be a bytes-like object (e.g. bytes or bytearray).
香肠蒸多久才能蒸熟
8
9 The byteorder argument determines the byte order ud to reprent the
10 integer. If byteorder is 'big', the most significant byte is at the
11 beginning of the byte array. If byteorder is 'little', the most
12 significant byte is at the end of the byte array. To request the native
13 byte order of the host system, u `sys.byteorder' as the byte order value.
大画恐龙
14
15 The signed keyword-only argument indicates whether two's complement is
16 ud to reprent the integer.
17 """
18 pass
如下举例:
1data = 100
2data_byte = _bytes(4, byteorder='little', signed=True)
3print(type(data_byte), data_byte)
4data_int = int.from_bytes(data_byte, byteorder='little', signed=True)
5print(type(data_int), data_int)
输出结果:
<class 'bytes'> b'd\x00\x00\x00'
<class 'int'> 100