python培训之int返回的方法探究

发布时间:2023-03-20 09:36:10 人气:67 作者:多测师

  python int返回的方法探究

  1、int额外的方法:

  int.bit_length()

  返回二进制表示整数所需的位数,不包括符号位和前面的零:

  >>> n = -37

  >>> bin(n)

  '-0b100101'

  >>> n.bit_length()

  6

python培训之int返回的方法探究

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

  (1024).to_bytes(2, byteorder='big')

  b'\x04\x00'

  (1024).to_bytes(10, byteorder='big')

  b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'

  (-1024).to_bytes(10, byteorder='big', signed=True)

  b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'

  x = 1000

  x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')

  b'\xe8\x03'

  以上就是python int返回的方法探究,希望对大家有所帮助。更多Python学习指路:请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线