在读取/写入二进制数据结构时访问位域 [英] Accessing bitfields while reading/writing binary data structures

查看:77
本文介绍了在读取/写入二进制数据结构时访问位域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为二进制格式编写解析器.这种二进制格式涉及不同的表,这些表又是二进制格式,通常包含不同的字段大小(其中的50-100之间).

I'm writing a parser for a binary format. This binary format involves different tables which are again in binary format containing varying field sizes usually (somewhere between 50 - 100 of them).

这些结构中的大多数都将具有位域,并且在用C表示时将看起来像这样:

Most of these structures will have bitfields and will look something like these when represented in C:

struct myHeader
{
  unsigned char fieldA : 3
  unsigned char fieldB : 2;
  unsigned char fieldC : 3;
  unsigned short fieldD : 14;
  unsigned char fieldE : 4
}

我碰到了struct模块,但意识到它的最低分辨率是一个字节,而不是一点点,否则该模块非常适合这项工作.

I came across the struct module but realized that its lowest resolution was a byte and not a bit, otherwise the module pretty much was the right fit for this work.

我知道使用ctypes支持位域,但是我不确定如何在此处连接包含位域的ctypes结构.

I know bitfields are supported using ctypes, but I'm not sure how to interface ctypes structs containing bitfields here.

我的另一种选择是自己操作这些位并将其馈送到字节中并与struct模块一起使用-但由于我有近50-100种不同类型的此类结构,因此为此编写代码会更容易出错.我还担心效率,因为该工具可能用于解析大千兆字节的二进制数据.

My other option is to manipulate the bits myself and feed it into bytes and use it with the struct module - but since I have close to 50-100 different types of such structures, writing the code for that becomes more error-prone. I'm also worried about efficiency since this tool might be used to parse large gigabytes of binary data.

谢谢.

推荐答案

使用比特串(您提到的您正在查看),它应该很容易实现.首先创建一些要解码的数据:

Using bitstring (which you mention you're looking at) it should be easy enough to implement. First to create some data to decode:

>>> myheader = "3, 2, 3, 14, 4"
>>> a = bitstring.pack(myheader, 1, 0, 5, 1000, 2)
>>> a.bin
'00100101000011111010000010'
>>> a.tobytes()
'%\x0f\xa0\x80'

然后再次对其进行解码

>>> a.readlist(myheader)
[1, 0, 5, 1000, 2]

您最关心的可能是速度.该库是经过优化的Python,但是速度不及C库.

Your main concern might well be the speed. The library is well optimised Python, but that's not nearly as fast as a C library would be.

这篇关于在读取/写入二进制数据结构时访问位域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆