根据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类型相互转化相关推荐

  1. C++ int与string类型相互转化

    转自:http://www.cnblogs.com/nzbbody/p/3504199.html int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制 ...

  2. C# Bitmap类型与Byte[]类型相互转化

    Bitmap   =>   byte[]  Bitmap b = new Bitmap( "test.bmp "); MemoryStream ms = new Memory ...

  3. int类型和byte类型的强制类型转换

    今天在读<Java网络编程>这本书的第二章 流 时,看到书中有一个地方关于int强制转换为byte类型时应注意的地方.这个地方有点细节,不过就应该把这种细节把握住. 情况是这样的,讲到In ...

  4. 为什么Byte类型的运算结果要用int类型来接收

    今天写程序的时候发现byte类型的数值进行运算无法用byte接收,必须是int类型的. 为什么Byte类型的运算结果要用int类型来接收? 我们给他编译一把: javac Add.java javap ...

  5. int和long类型取值范围。 基本数据类型 byte , short , char ,int , long , float ,double,boolean类型取值范围

    int和long类型取值范围. 1. int类型 ---------4字节(32位) ​ ------------------------------------------------------- ...

  6. Java初认识--基本数据类型(int 和byte之间赋值)默认值 类型强转

    Java简单介绍 Java面向对象:一个面向对象的木匠关心的制作的椅子:这个木匠干了什么事情 非面向对象的木匠关心的是所使用的工具. 狗吃粮 面向对象 吃狗粮 非面向对象 Java与c++区别: 1. ...

  7. *java* 在Java中给Int类型的最大值+1 ,以及int与byte之前强转的例子

    1.给int最大值+1 public class Test1{public static void main(String[] args){//保存当前int的最大值,同理也有long maxValu ...

  8. C#到Java byte类型冲突的解决

    最近要改写一个核心加密认证类,从C#改写成Java. 发现在调试时,加密的数据无论如何也对不上. 经过跟踪,发现问题出在C#和Java byte类型的区别上:在C#里 byte类型是无符号的,而Jav ...

  9. java int byte数组_Java 中int与byte数组转换详解

    1.与运算符的理解(&): 参加运算的两个数据,按二进位进行"与"运算.如果两个相应的二进位都为1,则该位的结果值为1,否则为0.即 0&0=0:0&1=0 ...

最新文章

  1. mysql8.0.12插件_mysql 8.0.12 安装使用教程
  2. 一文搞懂临床研究常见类型
  3. SAP Spartacus cost center Add按钮的href属性生成逻辑分析
  4. Java错别字检查_java中关于异常的处理
  5. 贪心算法之用优先队列解决最短路径问题(Dijkstra算法)
  6. 2个td合成一个td_18个月16个爆款,合成类玩法的下一个机会在哪?
  7. jquery获取checkbox是否选中
  8. number2chinese php,php 数字转换为中文
  9. 【网络基础】《TCP/IP详解》学习笔记2
  10. HiveServer2入门到熟悉
  11. Java基础梳理(一)
  12. How is NGINX Unit different from Apache?
  13. android组件不能加适配器,Android 针对继承BaseAdapter的自定义适配器应注意的几个地方...
  14. Eclipse安装WindowsBuilder失败的解决办法
  15. 2022年上半年系统集成项目管理工程师上午真题及答案解析
  16. 机械秒表的使用方法_秒表的使用方法!
  17. Android系统文件夹结构介绍
  18. LightOJ-1090-数论
  19. pytorch训练FER2013模型
  20. CV2逐步学习-2:cv2.GaussianBlur()详解

热门文章

  1. 工作五年以上的 UI 设计师都在干什么?
  2. 微信小程序登录注册的流程及实现思路
  3. Linux基础命令---zcat
  4. Mysql这么输入作文_我用access做数据库.最多可不可以在里面输入一篇作文啊?是不是要MYsql做啊?...
  5. mac调整启动台应用程序图标排列(几行几列)
  6. 判断二维点在线段的哪一侧
  7. JAVA FTP 上传文件
  8. inline内联函数
  9. 一维数组、数组排序、字符数组
  10. idea git 合并分支代码