python from_bytes()方法,转换bytes为十进制

from_bytes()方法

from_bytes()是python中int类型的内置方法,可以用于将参数给定的字节bytes类型数值转换为十进制的值。


from_bytes()语法及参数结构

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

提示:int不是变量,而是类型class,具体用法见下方实例代码。

参数解析表:

参数描述
bytesbytes类型变量
byteorder值为“big”和“little”两种,用于表示整数的字节顺序,big表示最高有效字节位于字节数组的开始位置,little则表示在末尾位置(仅供参考)。
signed值为True和False两种,True表示考虑符号位

from_bytes()返回值

bytes的十进制值。


from_bytes()实例代码

>>> b = b'\x01\x00'
>>> n1 = int.from_bytes(b,byteorder='little',signed=True)
>>> n1
1
>>> n2 = int.from_bytes(b, byteorder='big', signed=True)
>>> n2
256

全栈后端 / python教程 :


























Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.