int 和 字节 相互转换
In [10]: n = 0xf1f2
In [11]: bin(n)
Out[11]: '0b1111000111110010'
In [12]: n.bit_length()
Out[12]: 16
In [14]: n.to_bytes((n.bit_length() + 7) //8, 'little')
Out[14]: b'\xf2\xf1'
In [15]: n = 0x31f1
In [16]: n.to_bytes((n.bit_length() + 7) //8, 'little')
Out[16]: b'\xf11'
In [17]: type(n.to_bytes((n.bit_length() + 7) //8, 'little'))
Out[17]: bytes
In [18]: n.to_bytes((n.bit_length() + 7) //8, 'little').hex()
Out[18]: 'f131'
In [21]: n.to_bytes((n.bit_length() + 7) //8, 'big').hex()
Out[21]: '31f1'
In [49]: print(' '.join([hex(ch) for ch in n.to_bytes((n.bit_length()+7) // 8,'little')]))
0xf1 0x31
In [65]: print([hex(ch).replace('0x','') for ch in n.to_bytes((n.bit_length()+7) // 8,'little')])
['f1', '31']
In [40]: print(''.join([hex(byte).replace('0x', r'\x')for byte in n.to_bytes((n.bit_length() + 7) // 8, 'little')]))
\xf1\x31
int 和 字节 相互转换的更多相关文章
- 数据类型对应字节数(32位,64位 int 占字节数)
数据类型对应字节数(32位,64位 int 占字节数) 可用如sizeof(char),sizeof(char*)等得出 32位编译器: char :1个字节 char*(即指针变量): 4个字节(3 ...
- QString, string, int, char* 之间相互转换
这三种数据类型在实际运用中经常需要互相转换,那么这里小结下它们之间的转换方法: - Qstring & string Qt中封装的类十分强大,其成员函数数量之多比STD有过之而无不及,许多程序 ...
- java - Integer、int 、String相互转换总结
一下子还真记不清这三种数据类型之间的转换方法,所以做个小笔记. public class Test03 { public static void main(String[] args) { //int ...
- 8051单片机中访问int中字节的方法
在使用单片机中,unsigned int 占2个字节,unsigned char 占一个字节.而单片机是实行的字节寻址.16字节的bit寻址实在是不好用, 不好用在不能建数组. 在实际的开发过程中,要 ...
- int 和 string 相互转换(简洁版)
string int2str(int x) { return x ? num2str(x/10)+string(1,x%10+'0') : "";} int str2int(str ...
- [转载]C#中int和IntPtr相互转换
方法一. int转IntPtr int i = 12; IntPtr p = new IntPtr(i); IntPtr转int int myi = (int)p; ...
- C++: int和string相互转换
假设在一个C++的程序中常常会用到int和string之间的互换.个人建议能够写成一个函数,下次用的时候直接调用就可以. #include <iostream> #include < ...
- java和js中int和String相互转换常用方法整理
java中int和String的相互转换常用的几种方法: int > String int i=10;String s="";第一种方法:s=i+""; ...
- 文件-- 字节相互转换(word、图片、pdf...)
方式一: /// <summary> /// word文件转换二进制数据(用于保存数据库) /// </summary> /// <param name="wo ...
随机推荐
- PHP排序函数sort、rsort、asort、arsort、ksort、krsort
1.sort函数用于对数组元素值从低到高排序,去除原始索引元素,重新生成0,1,2..的键2.rsort函数用于对数组元素值从高到低排序,去除原始索引元素,重新生成0,1,2..的键3.asort函数 ...
- leetcode第一题(easy)
第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- vscode 将本地项目上传到码云
**************************************************************************************************** ...
- Maven下载清除jar包
maven jar包下载命令行方式 在STS中下载JAR包时经常卡住无法继续下载,这时可以用命令行方式进行下载.在终端中今入到该项目的根目录下,然后mvn clean;mvn install;等待下载 ...
- ResultEntity
就是封装的一个map集合 省时省力好用 package com.ujy.utils; import java.util.HashMap; import java.util.Map; public c ...
- Oracle锁表信息处理步骤
查看是否有锁表的sql select 'blocker(' || lb.sid || ':' || sb.username || ')-sql:' || qb.sql_text blockers, ' ...
- Spring基础06——依赖注入的一些细节
1.字面值 可用字符串表示的值,可以通过<value>元素标签或value属性进行注入.基本数据类型及其封装类,String类等类型都可以采取字面值注入的方式.若字面值包含特殊字符,可以使 ...
- Laplace's equation
链接:https://en.wikipedia.org/wiki/Laplace%27s_equation
- Centos7.5 rpm安装zabbix_agent4.0.3
1.下载并且安装 cd /data/tools/ ##切换到下载客户端目录 wget http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-ag ...
- C++ GUI Qt4学习笔记09
C++ GUI Qt4学习笔记09 qtc++ 本章介绍Qt中的拖放 拖放是一个应用程序内或者多个应用程序之间传递信息的一种直观的现代操作方式.除了剪贴板提供支持外,通常它还提供数据移动和复制的功 ...