- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个将写入二进制文件的 Python 应用程序。该文件将由在嵌入式目标上运行的一些 C 代码进行解析。
我相信我可以通过从 Struct class 派生来做到这一点,但是打包格式很糟糕,而且我所有的结构都是小端,所以我想到了使用 ctypes 包。
假设我有以下 C 结构:
struct my_c_struct
{
uint32_t a;
uint16_t b;
uint16_t table[];
};
在 C 端,我使用转换为内存缓冲区的指针对该结构进行操作,因此我可以:
uint8_t buf[128];
struct my_c_struct *p = (struct my_c_struct*) buf;
p->table[0] = 0xBEEF;
如何在 Python 中最好地表示它?我的第一个尝试是:
class MyCStruct(ctypes.LittleEndianStructure):
c_uint32 = ctypes.c_uint32
c_uint16 = ctypes.c_uint16
_pack_ = 1
_fields_ = [
("a", c_uint32),
("b", c_uint16),
]
def __init__(self, a, b):
"""
Constructor
"""
super(ctypes.LittleEndianStructure, self).__init__(a, b)
self.table = []
def pack(self):
data = bytearray(self.table)
return bytearray(self)+data
pack()
方法背后的想法是它将在结构的末尾组装可变长度表。请注意,我不知道 table
在创建对象时有多少条目。
我实现的方式显然行不通。所以我在考虑将 ctypes-devived 类嵌套在一个纯 Python 类中:
class MyCStruct:
class my_c_struct(ctypes.LittleEndianStructure):
_pack_ = 1
_fields_ = [ ("a", ctypes.c_uint32),
("b", ctypes.c_uint16) ]
def __init__(self, a, b):
"""
Constructor
"""
self.c_struct = self.my_c_struct(a,b)
self.table = []
def pack(self):
self.c_struct.b = len(self.table)
x = bytearray(self.c_struct)
y = bytearray()
for v in self._crc_table:
y += struct.pack("<H", v)
return x + y
这样做的好方法吗?我不想为了发现有更好的方法而深入兔子洞。
警告:我正在使用 Python 2(请不要问...),因此仅 Python 3 的解决方案对我没有用,但对宇宙的其他人有用。
干杯!
最佳答案
struct
模块对于这个问题真的很容易使用(Python 2 代码):
>>> import struct
>>> a = 1
>>> b = 2
>>> table = [3,4]
>>> struct.pack('<LH{}H'.format(len(table)),a,b,*table)
'\x01\x00\x00\x00\x02\x00\x03\x00\x04\x00'
使用.format
将16位值的长度插入到table
中,使用*table
展开table
转换为正确数量的参数。
用 ctypes
做这个更复杂。此函数声明一个具有正确可变数组大小的自定义结构并填充它,然后生成原始数据字节的字节字符串:
#!python2
from ctypes import *
def make_var_struct(a,b,table):
class Struct(Structure):
_pack_ = 1
_fields_ = (('a',c_uint32),
('b',c_uint16),
('table',c_uint16 * len(table)))
return Struct(a,b,(c_uint16*len(table))(*table))
s = make_var_struct(1,2,[3,4])
print(repr(''.join(buffer(s))))
输出:
'\x01\x00\x00\x00\x02\x00\x03\x00\x04\x00'
关于具有灵活数组成员的 Python ctypes 结构到 bytearray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63853622/
我有以下代码,适用于文本文件,但不适用于 pdf 文件。我的文件包含英语和希腊语字符。我尝试将 pdf 文件转换为 byteStream 并将 byteStream 转换为 String 格式,以便将
我有一些大的二进制文件,我需要在其中搜索特定的字节序列,例如: find_bytes = bytearray(base64.b16decode('a226fb42')) with open(filen
如何将 bytearray 值分配给面板背景图像。如果有人有想法或经验,请帮助我克服这个问题。简要经验: 我有面板控制,想加载从 webservice 获取的图像作为背景图像。所以我使用了 setst
我的 ByteArray 值为 avroBinaryValue ,架构名称值为 String schemaName ,上次修改日期值为 long 的 lastModifiedDate 。 byte[]
我试图在开始时将一个字节数组插入到另一个字节数组中。这是我正在努力完成的一个简单示例。 import struct a = bytearray(struct.pack(">i", 1)) b = by
我正在尝试将 long 和 bytearray 连接到另一个 bytearray。 我尝试过这样的: byte[] value1= new byte[16]; byte[] value2= new b
我有一个任务,我必须在数据库中读取和保存图像。 我正在使用 C#.net Webservice 和 SQlserver2008 数据库,并将图像保存为 varbinary 格式。 我想知道 如何从本地
我在创建 ByteArray var 时遇到了一些问题,其中它的元素也是 ByteArray,我不知道是不是可能先?以及如何? 最佳答案 ByteArray 顾名思义,就是一个字节数组。如果您想保留多
我有三个 bytearray,长度为 40000。我想将字节数组 1 的字节数组索引 0,1、bytearaay2 的索引 0,1 和 bytearray3 的索引合并到 40000 长度。 像这样:
我认为这是一个新手类型的问题,但我很理解这一点。 我可以找到很多关于如何将字符串转换为各种语言的字节数组的帖子。 我不明白的是逐个字符发生了什么。我知道屏幕上显示的每个字符都由一个数字表示,例如它的
锁定。这个问题及其答案是locked因为这个问题是题外话,但具有历史意义。它目前不接受新的答案或互动。 这是我的( Code Golf )挑战: 取两个字节数组并确定第二个数组是否是第一个数组的子字符
我在字节流方面做了很多工作,所以我理解这个概念——有些字节表示比其他字节更高的数值。 在阅读维基上的定义时,我变得非常困惑。 当我看到“Big-endian”或 BIG + END 这个词时,我期望最
我正在使用 Kafka 发送生产和消费消息。 生产很好,使用 制作人。 消费时,我使用下面的代码(取自示例)但我得到的每条记录只有 8 个字节(代码下方的示例输出)。 有没有一种方法可以让消费者简单地
我有很多时间创建缩略图,然后将它们转换为字节数组。我尝试了三种方法,所有 3 次都出现错误。 第一个是使用Bitmap.GetThumbnailImage,我过去用过,然后直接保存到Response.
如何将存储文件分配给 ByteArray? var file = await openPicker.PickSingleFileAsync(); 最佳答案 这里是一个使用文件选择器的示例,获取存储文件
我需要在Kotlin中创建一个包含混合类型的字节数组。 具体来说,我需要编写如下内容: 0, 1, 0x02, 1, "me@emailaddr.com" 使用Kotlin实现BLE的特性。 显然,我
当我运行这个: class SomeTest { fun howToMockByteArray() { val bytes = Mockito.mock(ByteArray::
D中ubyte[]的最大大小有限制吗? 在this线程 我遇到内存不足错误,无法理解是驱动程序问题还是 ubyte[] 大小限制? 最佳答案 D中任意数组的最大长度为size_t.max。但是,由于
如果只有一个写入器将字节附加到一个bytearray,并且多个读取器从中读取数据,那么是否有可能一个或多个读取器读取既不在扩展之前也不在扩展之后的数据? 例如,如果bytearray中的旧数据是012
请耐心听我说;我是系统管理员而不是开发人员。下面的代码对我来说效果很好。但是当我将其分成两个文件以便类位于一个文件中而逻辑位于另一个文件中时,我收到一条错误,指出 data[0] 是 str 并且不支
我是一名优秀的程序员,十分优秀!