pythonbyte转int_Python int与byte类型相互转化
根据Python自定义的功能,使用to_bytes函数转化int类型数据为byte型,然后使用from_bytesa将byte类型数据转化为int型。
def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
"""
int.to_bytes(length, byteorder, *, signed=False) -> bytes
Return an array of bytes representing an integer.
The integer is represented using length bytes. An OverflowError is
raised if the integer is not representable with the given number of
bytes.
The byteorder argument determines the byte order used to represent the
integer. If byteorder is 'big', the most significant byte is at the
beginning of the byte array. If byteorder is 'little', the most
significant byte is at the end of the byte array. To request the native
byte order of the host system, use `sys.byteorder' as the byte order value.
The signed keyword-only argument determines whether two's complement is
used to represent the integer. If signed is False and a negative integer
is given, an OverflowError is raised.
"""
pass
def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
"""
int.from_bytes(bytes, byteorder, *, signed=False) -> int
Return the integer represented by the given array of bytes.
The bytes argument must be a bytes-like object (e.g. bytes or bytearray).
The byteorder argument determines the byte order used to represent the
integer. If byteorder is 'big', the most significant byte is at the
beginning of the byte array. If byteorder is 'little', the most
significant byte is at the end of the byte array. To request the native
byte order of the host system, use `sys.byteorder' as the byte order value.
The signed keyword-only argument indicates whether two's complement is
used to represent the integer.
"""
pass
如下举例:
data = 100
data_byte = data.to_bytes(4, byteorder='little', signed=True)
print(type(data_byte), data_byte)
data_int = int.from_bytes(data_byte, byteorder='little', signed=True)
print(type(data_int), data_int)
输出结果:
b'd\x00\x00\x00'
100
pythonbyte转int_Python int与byte类型相互转化相关推荐
- C++ int与string类型相互转化
转自:http://www.cnblogs.com/nzbbody/p/3504199.html int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制 ...
- C# Bitmap类型与Byte[]类型相互转化
Bitmap => byte[] Bitmap b = new Bitmap( "test.bmp "); MemoryStream ms = new Memory ...
- int类型和byte类型的强制类型转换
今天在读<Java网络编程>这本书的第二章 流 时,看到书中有一个地方关于int强制转换为byte类型时应注意的地方.这个地方有点细节,不过就应该把这种细节把握住. 情况是这样的,讲到In ...
- 为什么Byte类型的运算结果要用int类型来接收
今天写程序的时候发现byte类型的数值进行运算无法用byte接收,必须是int类型的. 为什么Byte类型的运算结果要用int类型来接收? 我们给他编译一把: javac Add.java javap ...
- int和long类型取值范围。 基本数据类型 byte , short , char ,int , long , float ,double,boolean类型取值范围
int和long类型取值范围. 1. int类型 ---------4字节(32位) ------------------------------------------------------- ...
- Java初认识--基本数据类型(int 和byte之间赋值)默认值 类型强转
Java简单介绍 Java面向对象:一个面向对象的木匠关心的制作的椅子:这个木匠干了什么事情 非面向对象的木匠关心的是所使用的工具. 狗吃粮 面向对象 吃狗粮 非面向对象 Java与c++区别: 1. ...
- *java* 在Java中给Int类型的最大值+1 ,以及int与byte之前强转的例子
1.给int最大值+1 public class Test1{public static void main(String[] args){//保存当前int的最大值,同理也有long maxValu ...
- C#到Java byte类型冲突的解决
最近要改写一个核心加密认证类,从C#改写成Java. 发现在调试时,加密的数据无论如何也对不上. 经过跟踪,发现问题出在C#和Java byte类型的区别上:在C#里 byte类型是无符号的,而Jav ...
- java int byte数组_Java 中int与byte数组转换详解
1.与运算符的理解(&): 参加运算的两个数据,按二进位进行"与"运算.如果两个相应的二进位都为1,则该位的结果值为1,否则为0.即 0&0=0:0&1=0 ...
最新文章
- mysql8.0.12插件_mysql 8.0.12 安装使用教程
- 一文搞懂临床研究常见类型
- SAP Spartacus cost center Add按钮的href属性生成逻辑分析
- Java错别字检查_java中关于异常的处理
- 贪心算法之用优先队列解决最短路径问题(Dijkstra算法)
- 2个td合成一个td_18个月16个爆款,合成类玩法的下一个机会在哪?
- jquery获取checkbox是否选中
- number2chinese php,php 数字转换为中文
- 【网络基础】《TCP/IP详解》学习笔记2
- HiveServer2入门到熟悉
- Java基础梳理(一)
- How is NGINX Unit different from Apache?
- android组件不能加适配器,Android 针对继承BaseAdapter的自定义适配器应注意的几个地方...
- Eclipse安装WindowsBuilder失败的解决办法
- 2022年上半年系统集成项目管理工程师上午真题及答案解析
- 机械秒表的使用方法_秒表的使用方法!
- Android系统文件夹结构介绍
- LightOJ-1090-数论
- pytorch训练FER2013模型
- CV2逐步学习-2:cv2.GaussianBlur()详解