(转)python之from_bytes、to_bytes
原文:https://blog.csdn.net/PYTandFA/article/details/78741339
https://python3-cookbook.readthedocs.io/zh_CN/latest/c03/p05_pack_unpack_large_int_from_bytes.html
首先我们来看两个__builtin__函数
num1 = int.from_bytes(b'12', byteorder = 'big')
num2 = int.from_bytes(b'12', byteorder = 'little')
print('(%s,'%'num1', num1, '),', '(%s,'%'num2', num2, ')')
result:(num1, 12594 ), (num2, 12849 )
byt1 = (1024).to_bytes(2, byteorder = 'big')
byt2 = (1024).to_bytes(10, byteorder = 'big')
byt3 = (-1024).to_bytes(10, byteorder= 'big')
lis1 = ['byt1', 'byt2', 'byt3', 'byt4']
lis2 = [byt1, byt2, byt3, byt4]
lis3 = zip(lis1, lis2)
dic = dict(lis3)
print(dic)
result:
byt1': b'\x04\x00'
byt2': b'\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00'
byt3': b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
byt4': b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'int.from_bytes()功能是将字节转化成int型数字'12'如果没有标明进制,看做ascii码值,'1' = 49 = 0011 0001, '2' = 50 = 0011 0010,如果byteorder = 'big', b'12' = 0010 0001 0010 0010 = 12594;如果byteorder = 'littlele', b'12' = 0011 0010 0011 0001 = 12849。第三个参数为signed表示有符号和无符号;(number).to_bytes()功能将整数转化成byte
(1024).to_bytes(10, byteorder = 'big'),一个int型,4字节。1024 = 0000 0000 0000 0000 0000 0100 0000 0000,由于给定的是10,所以凑齐10个字节,高位用6个
0000 0000占位,如果最后用16进制表示,1024 = b'\x00\x00\x00\x00\x00\x00\x00\x00x04\x00
在看一个例子:
byt3 = (-1024).to_bytes(10, byteorder= 'big', signed = 'true'),由于signed = 'true', -1024 = 1000 ...(11) 0000 0000 0000 0000 0000 0100 0000 0000,符号位为1,...省略了
11个0000,由于负数由补码表示,所以先求-1024的反码,即符号位不变,其他位0变1,1变0,得:1111 ...(11) 1111 1111 1111 1111 1111 1011 1111 1111,对反码 + 1,得到补码:
1111 ...(11) 1111 1111 1111 1111 1111 1100 0000 0000,用16进制表示:\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00
再举个例子:
num3 = int.from_bytes(b'\xf3\x25', byteorder = 'little')
f3 = 243(10进制)= 1111 0011,25 = 37(10进制)= 0010 0101,byteorder = 'little',字节的低位占主要作用, 得到:0010 0101 1111 0011,得到十进制:9715
num3 = int.from_bytes(b'\xf3\x25', byteorder = 'big', signed = 'true')
f3 = 243(10进制)= 1111 0011,25 = 37(10进制)= 0010 0101,byteorder = 'big',字节的高位占主要作用, 得到:1111 0011 0010 0101,signed = 'true',说明有符
号,而且高位为1,所以用补码:1000 1100 1101 1011 即:-3291
(转)python之from_bytes、to_bytes的更多相关文章
- 【Teradata SQL】十进制转换成二进制
1.数值类型转换为二进制(TO_BYTE+FROM_BYTES) sel FROM_BYTES(TO_BYTE(),'base2'); 2.字符串类型转换为二进制(TO_BYTES+FROM_BYT ...
- Python之scrapy框架之post传输数据错误:TypeError: to_bytes must receive a unicode, str or bytes object, got int
错误名:TypeError: to_bytes must receive a unicode, str or bytes object, got int 错误翻译:类型错误:to_bytes必须接收u ...
- Python基础(二)
本章内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典.set集合) for 循环 enumrate range和x ...
- python基础之数据类型(一)
Python3 数字(Number) 定义:a=1 特性: 1.只能存放一个值 2.一经定义,不可更改 3.直接访问 分类:整型,长整型,布尔,浮点,复数 python2.*与python3.*关于整 ...
- Python基础2
入门知识拾遗 一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 1==1: name = 'yuxiaozheng' print name 外层变量,可以被 ...
- Python全栈开发【基础二】
Python全栈开发[基础二] 本节内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典) 其他(编码,range,f ...
- python之路3:
class set(object): """ set() -> new empty set object set(iterable) -> new set o ...
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- python初识第二篇
python 编码: 第一次编程有时候会遇到乱码的情况,就可以通过以下的情况来解决 在Windows中默认的就是gbk编码,如果在代码头两部定义utf-8,系统还会按照系统的方式来定义. python ...
随机推荐
- Spring Boot项目Maven Build报错的解决方法
问题1, [ERROR]Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test (defau ...
- python的6种基本数据类型--字典
python的6种基本数据类型--字典 字典 字典的定义与特性 字典是Python语言中唯一的映射类型. 定义:{key1:value1,key2:value2} 1.键与值用冒号":& ...
- c# devexpress绘图 三角函数
加标题: using System; using System.Windows.Forms; using DevExpress.XtraCharts; // ... namespace SideByS ...
- SHELL脚本之awk妙用
对于一个sougou文本文件,解压后大概4G,要求在其基础上切出第一列时间年月日时分秒增加在列中,作为hive的一个索引.先将文件head一下展示格式: [root@Master date]# hea ...
- POJ 2433 Landscaping (贪心)
题意:给定一个序列表示一群山,要你保留最多 K 个山峰,最少要削去多少体积和土.一个山峰是指一段连续的相等的区间,并且左边和右边只能比这个区间低,或者是边界. 析:贪心,每次都寻找体积最小的山峰,然后 ...
- sql计算经纬度得出最近距离的公式
sql计算经纬度得出最近距离的公式 //根据经纬度计算两点距离 mappoint //数据库已有字段,商家经纬度 实例:113.272148,23.147299 $lon = "" ...
- Ng第四课:多变量线性回归(Linear Regression with Multiple Variables)
4.1 多维特征 4.2 多变量梯度下降 4.3 梯度下降法实践 1-特征缩放 4.4 梯度下降法实践 2-学习率 4.5 特征和多项式回归 4.6 正规方程 4.7 正规方程及不可逆性 ...
- C#-vs2012学习笔记-惊奇于vs的强大和便利
网站常用功能自动完成,包括网页和数据库.
- Alpha阶段敏捷冲刺(五)
1.站立式会议 提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 祁泽文:实现了个人遗忘曲线图 徐璐琳:完成了微信Web ...
- Leetcode--1. Two Sum(easy)
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...