我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组,字典。但是C语言中有些字节型的变量,在python中该如何实现呢?这点颇为重要,特别是要在网络上进行数据传输的话。
struct.pack(fmt, v1, v2, …)
Return a string containing the values v1, v2, … packed according to the given format. The arguments must match the values required by the format exactly.
野生翻译:返回一个包含v1,v2的,根据所给fmt打包的字符串,其中的参数必须和fmt要求的值匹配
重点来理解一下fmt:
一、下面是讲fmt的第一个字符:
By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).
野生翻译:默认情况下,C类型在机器的本机格式和字节顺序中表示,如果需要的话,可以通过跳过pad字节来正确地对齐(根据C编译器使用的规则)。
Alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:
野生翻译:可以使用格式字符串的第一个字符来指示填充数据的字节顺序、大小和对齐方式(这是可选的),如下表所示
If the first character is not one of these, ‘@’ is assumed.
野生翻译:如果第一个字符不是上面表里面中的一个,@是默认的值
Native byte order is big-endian or little-endian, depending on the host system. For example, Intel x86 and AMD64 (x86-64) are little-endian; Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature switchable endianness (bi-endian). Use sys.byteorder to check the endianness of your system.
野生翻译:原生字节顺序是big-endian还是little-endian,取决于主机系统。例如,Intel x86和AMD64(x86-64)是little-endian ;摩托罗拉68000和PowerPC G5是big-endian; ARM和英特尔的Itanium特性是可切换的(双endian)。可以使用sys.byteorder指令来检查你系统是按照什么排序的
Native size and alignment are determined using the C compiler’s sizeof expression. This is always combined with native byte order.
Standard size depends only on the format character; see the table in the Format Characters section.
Note the difference between ‘@’ and ‘=’: both use native byte order, but the size and alignment of the latter is standardized.
The form ‘!’ is available for those poor souls who claim they can’t remember whether network byte order is big-endian or little-endian.
There is no way to indicate non-native byte order (force byte-swapping); use the appropriate choice of ‘<’ or ‘>’.
野生翻译:本机大小和对齐是使用C编译器的sizeof表达式来确定的。这总是与本机字节顺序相结合。
标准大小只取决于格式字符;请参见格式字符部分中的表格。
注意“@”和“=”之间的区别:两者都使用本机字节顺序,但是后者的大小和对齐是标准化的。
表单”!“对于那些不记得网络字节顺序是big-endian或little-endian的字节顺序的人来说是可行的。”
没有办法指出非本地字节顺序(强制字节交换);使用“<”或“>”的适当选择。
Notes:
野生翻译:
注意:
1. 填充只在连续的结构成员之间自动添加。在编码的结构体的开始或结束时没有添加填充物。
2. 在使用非本地大小和对齐方式时,不添加任何填充,例如“<”、“”、“=”和“!”。
3. 为了使结构的结束与特定类型的对齐要求保持一致,以重复计数为零的方式结束该类型的代码。详见examples
二、 下面是讲fmt后面的内容
Format characters have the following meaning; the conversion between C and Python values should be obvious given their types. The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of ‘<’, ‘>’, ‘!’ or ‘=’. When using native size, the size of the packed value is platform-dependent.
野生翻译:fmt字符有以下含义;C和Python值之间的转换应该是明显的,因为它们提供的类型。“标准大小”列指的是在使用标准尺寸时以字节为单位的包装值的大小;也就是说,当fmt以“<”、“”、“!”开头的时候。”或“=”。当使用本机大小时,包装值的大小与平台相关。
来源:网络
本文主要针对结构体内存对齐,python Struct,以及二进制文件解析做总结。 一、结构体内存对齐 1、为什么要内存对齐 (1)平台移植性好 不是所有硬件平台都能访问任意地址上的数据,某些硬件平台只能在某些地址访问某些特定类型的数据,否在抛出硬件异常,即遇到未对齐的边界值就直接不进行读取数据了。 (2)cpu处理效率高 用空间换取时间,提高效率。 举个例子:如下结构体,cpu对齐单位4 如果不...
PHP中有两个函数pack和unpack,很多PHPer在实际项目中从来没有使用过,甚至也不知道这两个方法是用来干嘛的。这篇文章来为大家介绍一下它俩到底是用来干啥的。 pack 该函数用来将对应的参数($args)打包成二进制字符串。 其中第一个参数$format,有如下选项(可选参数很多,后面会选几个常用的讲解): Code Description a 以NUL字节填充字符串空白 A 以SPAC...
转载大牛的链接,理解原码、补码之间的关系 https://blog.csdn.net/luolaifa000/article/details/83010807 首先,理解python中,计算机只存储整数的补码(不是原码),屏幕上显示的都是整数的补码(此处重要,黑客帝国Neo看到的都是01010101.) 其次,强记 正数的补码等于原码;负数的补码等于原码符号位不变,其余各位取反加1 贴张图加深理解...
每次x264_encoder_encode被调用,h->out.i_nal都是1,如下图所示: 在该函数开始处插入:printf("\n( encoder.c:x264_encoder_encode )h->out.i_nal = %d ",h->out.i_nal);//zjh  ...
Blender 插件之 Blender for UE4 https://zhuanlan.zhihu.com/p/146665394 Blender 插件之 Blender for UE4 WeArt微创意 腾讯科技有限公司 游戏美术 怎么使用? 使用Blender处理虚幻引擎4的对象包可能很繁琐。这就是为什么我创建加载项:“ Blende...
1.安装好了在crm命令行里输入: sqlplus system/密码 看到如下提示就证明安装成功: 还有一种连接是基于网络通过监听器来完成连接的: 输入:sqlplus system/密码@127.0.0.1:1521/xe 2.继续来配置plsq Developer 进去之后在工具一栏选择:首选项 在其目录下配置路径如下: 连接成功之后以system的权限去登录,并且可以创建用户,...
Flink的安装 Flink的相关安装步骤如下: 装虚拟机 装系统 装jdk 装scala(不需要不用) 装Hadoop(不需要不用) 装Flink 配置环境变量 如果只是刚开始的自我测试,安装还是很简单的,直接下载包,上传服务器,tar解压,配置了环境变量,source一下,ok,可以用了,这时不放start-cluster.sh一下启动flink吧(这里只是测试,安装了...
这属于中文不支持问题 只需在代码前加一行注释 #coding=utf-8 #encoding = utf-8 #-*-coding:utf-8-*- 任选一个均可,但最好用最后一个,它是正规的Python推荐写法 如果采用是的PyCharm,加注释还不行,可点击File ->Default Settings->File Encoding 改成utf-8。 如果还是不行。返...
最近做的项目使用mvc+webapi,采取前后端分离的方式,后台提供API接口给前端开发人员。这个过程中遇到一个问题后台开发人员怎么提供接口说明文档给前端开发人员,最初打算使用word文档方式进行交流,实际操作中却很少动手去写。为了解决这个问题,特意在博客园中搜索了一下api接口文档生成的文章,引起我注意的有两种方案。1.微软自带的Microsoft.AspNet.WebApi.HelpPag...
目录 写在前面的话 一、广播 (一)广播接收者 (二)自定义广播的发送与接收 (三)有序广播和无序广播 二、服务 写在前面的话 1、主要参考自:https://b23.tv/Flmxaa 2、内容如果有不对的,希望可以指出或补充。 3、新知识。 一、广播 (一)广播接收者 ① 概述 广播接收者(BroadcastReceiver):Android系统中内置了很多广播,列如手机的开机完成、电池电量不...
I am looking for spinner in wicket which should be simillar as JSpinner in java swing. I found class: http://www.jarvana.com/jarvana/view/org/wicketstuff/minis/1.4.9/minis-1.4.9-javadoc.jar!/org/wicke...
I am trying to implement selecting individual elements on the click of the mouse in a html page. When clicking, I want to be able to find which element I am clicking on. The end goal is to be able to ...
Possible Duplicate: Does anyone know of a good JSON time server? Is there any public json or xml present on server which I can parse for current time? I shall use this time for checking the expiration...
The _TABLE_SUFFIX feature is great and exactly what I was looking for to solve my problem - however it is scanning all of the data matched by the wildcard when I use a sub-query to determine which tab...
I have tried a lot of different grouping and ordering syntax but I am really struggling to get what I need. I am trying to order by DATE DESC, but I also want the PROJECTS to stick together (no matter...