演示代码

num = 7print(num.bit_length()) num = -7print(num.bit_length())

输出结果为:

33

phpinttobyte一日一技python位函数intbitlengthtobytes和frombytes Angular

2. int.to_bytes(length, byteorder, , signed=False)

返回表示整数的字节数组。

代码演示:

# Returns byte representation of 1024 in a # big endian machine. print((1024).to_bytes(2, byteorder ='big'))

输出结果为:

b'\x04\x00'

3. int.from_bytes(bytes, byteorder, , signed=False)

返回由给定的字节数组表示的整数,代码演示:

# Returns integer value of '\x00\x10' in big endian machine. print(int.from_bytes(b'\x00\x10', byteorder ='big'))

输出结果为:

16