python的int、bit和byte操作

1、python的bin()函数

作用:将一个整数转变为一个前缀为“0b”的二进制字符串(返回的结果是一个字符串类型)

>>> bin(3)
'0b11'
>>> bin(-5)
'-0b101'

去除前面的0b前缀方法

>>> format(3, 'b')
'11'
>>> format(3,'#b')
'0b11'
>>> bin(3)[2:]
'11'

2、python的bytes操作函数

bytes.hex() #将bytes类型转换成字符串(每个位置的字符由ascci码表示)

bytes.fromhex() #将16进制字符串转换成bytes类型

下面三者有时会转换,其中十六进制字符串是桥梁

  • 整数

  • 十六进制字符串

  • bytes类型

这三者的转换通过hex函数来连接

>>> a = "Hello".encode()
>>> b = bytes.hex(a)
>>> b
'48656c6c6f'
>>> c = int(b, 16)
>>> c
310939249775
>>> d = hex(c)[2:]
>>> d
'48656c6c6f'
>>> e = bytes.fromhex(d)
>>> e
b'Hello'

3、int 的 bytes_from 和 to_bytes方法

可以用dir方法查看

>>> dir(int)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
  • dir 函数如果没有实参,则返回当前本地作用域中的名称列表。如果有实参,它会尝试返回该对象的有效属性列表。

int.to_bytes()内置函数

int.to_bytes(length, byteorder, *, signed=False)
#返回表示一个整数的字节数组,如果给定的长度表示不了该整数,会发生溢出
#length  返回的字节长度,也就是返回length个字节
#byteorder  返回的是字节对齐的方式  big 表示大端  little 表示小端
#signed 参数确定是否使用二的补码来表示整数。 如果 signed 为 False 并且给出的是负整数,则会引发 OverflowError。 signed 的默认值为 False。

#实例:
>>> (1024).to_bytes(2, byteorder='big')
b'\x04\x00'
>>> (-1024).to_bytes(10, byteorder='big', signed='True')
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> (-1024).to_bytes(10, byteorder='big')
'''
发生报错信息如下
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    (-1024).to_bytes(10, byteorder='big')
OverflowError: can't convert negative int to unsigned
'''

int.bytes_from()内置函数0

int.bytes_from(bytes, byteorder, *, signed=False)
#返回由给定字节数组所表示的整数。
#bytes 这个参数有要求,必须是 bytes-like object 或是生成字节的可迭代对象
# byteorder 这个参数指示字节的对齐方式, big表示大端对齐, little表示小端对齐
#signed 指示是否用二进制补码表示,默认为False,即不用补码表示

>>> int.from_bytes(b'\x00\x10', byteorder='big')
16
>>> int.from_bytes(b'\x00\x10', byteorder='little')
4096
>>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)
-1024
>>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)
64512
>>> int.from_bytes([255, 0, 0], byteorder='big')
16711680
>>> (16711680).to_bytes(3, byteorder='big')
b'\xff\x00\x00'
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值