序列化和反序列化位字段

发布于 2025-01-03 10:11:14 字数 742 浏览 0 评论 0 原文

我收到了一份文档,其中定义了通过串行通信通道传输和接收的一组消息。我想获取传入消息并将它们反序列化为对象,并序列化我的出站消息。线路上的编码是已建立且不可更改的,并且由标头中的各种位字段和不同的有效负载组成,例如,

class Message{
int msg_num : 7
int dest_addr : 4
bool SRR : 1
bool IDE : 1
int source_addr : 6
//... and so on...
}

我查看了使用 protobufs,但似乎建立了它们的 varint 编码方法。我还查看了 boost-serialization ,但根据我到目前为止所读到的内容,编码是如何完成的并不完全清楚。

那么,有几个问题:

  • 我可以使用 boost-serialization 将字节流转换为对象吗?
  • 为了不必推出我自己的序列化例程(维护混乱),是否有一种首选机制来完成我的任务(例如,自定义boost-serialization Archive,这是我已经使用过的另一种方法)没有发现)

I've been handed a document that defines a set of messages that are transmitted and received over a serial communications channel. I'd like to take the incoming messages and deserialize them into objects, and serialize my outbound messages as well. The encoding over the wire is established and not changeable, and consists of various bitfields in the header and varying payloads, e.g.,

class Message{
int msg_num : 7
int dest_addr : 4
bool SRR : 1
bool IDE : 1
int source_addr : 6
//... and so on...
}

I took a look at using protobufs, but it appears that their varint method of encoding is established. I've also looked at boost-serialization, but based on what I've read so far, how the encoding is done there is not entirely clear.

So, a few questions:

  • Can I use boost-serialization to convert my bytestream to objects?
  • With a goal of not having to roll my own routines for serialization (a maintenance mess), is there a preferred mechanism for accomplishing my task (e.g., a custom boost-serialization Archive, another method I've not discovered)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2025-01-10 10:11:14

我认为您不会找到与您正在查看的自定义协议相匹配的易于使用的序列化器。但看起来您拥有的原语集(int,bool + size)足够简单,可以编写您自己的解码器/编码器。根据收到的消息简单地生成 C/C++ 代码。生成采用此类描述的可编译代码应该是相当简单的任务。它应该是在编译时完成的自动生成 - 类似于 protobuf/Corba 正在做的事情。

示例:根据规范:

class Message{
    int msg_num : 7
    int dest_addr : 4
    bool SRR : 1
    bool IDE : 1
    int source_addr : 6
    //... and so on...
}

转换器可以编写一个函数,其主体类似于(抽象符号并假设 MSB):

解码器:

m = new Message()
{
    long long val = 0
    for(int i=0; i<7; i++) {
        val <<= 8
        val += nextByte()    
    }
    m.msg_num = val
}
{
    long long val = 0
    for(int i=0; i<4; i++) {
        val <<= 8
        val += nextByte()    
    }
    m.dest_addr = val
}
{
    int val = nextByte()
    m.SRR = val
}
{
    int val = nextByte()
    m.IDE = val
}
{
    long long val = 0
    for(int i=0; i<6; i++) {
        val <<= 8
        val += nextByte()    
    }
    m.source_addr = val
}
// and so on

编码器:

{
    long long val = m.msg_num
    for(int i=0;i<7;i++) {
        writeByte(val & 0xFF)
        val >>= 8
    }
}
{
    long long val = m.dest_addr
    for(int i=0;i<4;i++) {
        writeByte(val & 0xFF)
        val >>= 8
    }
}
....

这应该很容易生成,也是确保编码是自定义的最简单方法。

I think you won't find an easy-to-use serializer that will match the custom protocol you are looking at. But it look the set of primitives you have (int, bool + size ) are simple enough to be able to write your own decoder/encoder. Simply generating C/C++ code based on the message received. It should be fairly simple task to generate a compilable code taking such description. It should be an automated generation done at the compile time - similar to what protobuf/Corba are doing.

Example: from the specification:

class Message{
    int msg_num : 7
    int dest_addr : 4
    bool SRR : 1
    bool IDE : 1
    int source_addr : 6
    //... and so on...
}

the converter could write a function with body similar to (abstract notation and assuming MSB):

Decoder:

m = new Message()
{
    long long val = 0
    for(int i=0; i<7; i++) {
        val <<= 8
        val += nextByte()    
    }
    m.msg_num = val
}
{
    long long val = 0
    for(int i=0; i<4; i++) {
        val <<= 8
        val += nextByte()    
    }
    m.dest_addr = val
}
{
    int val = nextByte()
    m.SRR = val
}
{
    int val = nextByte()
    m.IDE = val
}
{
    long long val = 0
    for(int i=0; i<6; i++) {
        val <<= 8
        val += nextByte()    
    }
    m.source_addr = val
}
// and so on

Encoder:

{
    long long val = m.msg_num
    for(int i=0;i<7;i++) {
        writeByte(val & 0xFF)
        val >>= 8
    }
}
{
    long long val = m.dest_addr
    for(int i=0;i<4;i++) {
        writeByte(val & 0xFF)
        val >>= 8
    }
}
....

That should be pretty easy to generate and the simplest way to make sure the encoding is custom.

撩发小公举 2025-01-10 10:11:14

如果您仅限于单一平台(即仅限于单字节顺序),并且消息是 POD 类型,您可以将消息声明为primitive

否则,至少在 boost.serialization 的情况下,您必须编写代码,即“序列化例程”。它至少支持字节顺序转换

[编辑]
错了,这不是原始的,我迷失在序列化文档的深处

In case you're limited to single platform only (i.e. limited with single byte order), and Message is POD type, you can declare your message as primitive.

Otherwise, in case of boost.serialization at least, you'll have to write code, i.e. 'routines for serialization'. It supports byte order conversions, at least

[Edit]
Wrong, it's not primitive, I'm lost in the depths of serialization docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文