<small id='qpyb6'></small><noframes id='qpyb6'>

      <tfoot id='qpyb6'></tfoot>
        <legend id='qpyb6'><style id='qpyb6'><dir id='qpyb6'><q id='qpyb6'></q></dir></style></legend>
          <bdo id='qpyb6'></bdo><ul id='qpyb6'></ul>
      1. <i id='qpyb6'><tr id='qpyb6'><dt id='qpyb6'><q id='qpyb6'><span id='qpyb6'><b id='qpyb6'><form id='qpyb6'><ins id='qpyb6'></ins><ul id='qpyb6'></ul><sub id='qpyb6'></sub></form><legend id='qpyb6'></legend><bdo id='qpyb6'><pre id='qpyb6'><center id='qpyb6'></center></pre></bdo></b><th id='qpyb6'></th></span></q></dt></tr></i><div id='qpyb6'><tfoot id='qpyb6'></tfoot><dl id='qpyb6'><fieldset id='qpyb6'></fieldset></dl></div>

      2. 结构体的序列化

        时间:2023-10-06
        <i id='nLAX4'><tr id='nLAX4'><dt id='nLAX4'><q id='nLAX4'><span id='nLAX4'><b id='nLAX4'><form id='nLAX4'><ins id='nLAX4'></ins><ul id='nLAX4'></ul><sub id='nLAX4'></sub></form><legend id='nLAX4'></legend><bdo id='nLAX4'><pre id='nLAX4'><center id='nLAX4'></center></pre></bdo></b><th id='nLAX4'></th></span></q></dt></tr></i><div id='nLAX4'><tfoot id='nLAX4'></tfoot><dl id='nLAX4'><fieldset id='nLAX4'></fieldset></dl></div>

          <tbody id='nLAX4'></tbody>

          <legend id='nLAX4'><style id='nLAX4'><dir id='nLAX4'><q id='nLAX4'></q></dir></style></legend>

            <small id='nLAX4'></small><noframes id='nLAX4'>

                • <bdo id='nLAX4'></bdo><ul id='nLAX4'></ul>

                  <tfoot id='nLAX4'></tfoot>
                  本文介绍了结构体的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我有一个结构体,我想使用 winsock 2 通过网络将其成员值发送到另一个系统.我使用的是 C++ 语言.我如何将它转换为 char * 记住结构必须在发送之前序列化,以及如何在另一端将 char * 反序列化为结构?我发现 boost 序列化是对类似问题的建议,但谁能用一个小代码片段来说明序列化和反序列化?

                  Suppose i have a struct whose member values i want to send over the network to another system using winsock 2. I'm using C++ language. How do i convert it to char * keeping in mind that the struct has to be serialized before sending and also how do i deserialize the char * into struct at the other end? I found boost serialization as a suggestion to similar question but can anyone illustrate with a small code snippet for both serialization and deserialization ?

                  这个问题可能看起来很基本,但相关帖子的其他答案并没有太大帮助.

                  This question might seem very basic but the other answers to the related posts did not help much.

                  推荐答案

                  以下示例显示了将 struct 序列化为 char 数组并对其进行反序列化的最简单方法.

                  Following example shows a simplest way to serialize struct into char array and de-serialize it.

                  #include <iostream>
                  #include <cstring>
                  
                  #define BUFSIZE 512
                  #define PACKETSIZE sizeof(MSG)
                  
                  using namespace std;
                  
                  typedef struct MSG
                  {
                      int type;
                      int priority;
                      int sender;
                      char message[BUFSIZE];
                  }MSG;
                  
                  void serialize(MSG* msgPacket, char *data);
                  void deserialize(char *data, MSG* msgPacket);
                  void printMsg(MSG* msgPacket);
                  
                  int main()
                  {
                      MSG* newMsg = new MSG;
                      newMsg->type = 1;
                      newMsg->priority = 9;
                      newMsg->sender = 2;
                      strcpy(newMsg->message, "hello from server");
                      printMsg(newMsg);
                  
                      char data[PACKETSIZE];
                  
                      serialize(newMsg, data);
                  
                      MSG* temp = new MSG;
                      deserialize(data, temp);
                      printMsg(temp);
                  
                      return 0;
                  }
                  
                  void serialize(MSG* msgPacket, char *data)
                  {
                      int *q = (int*)data;    
                      *q = msgPacket->type;       q++;    
                      *q = msgPacket->priority;   q++;    
                      *q = msgPacket->sender;     q++;
                  
                      char *p = (char*)q;
                      int i = 0;
                      while (i < BUFSIZE)
                      {
                          *p = msgPacket->message[i];
                          p++;
                          i++;
                      }
                  }
                  
                  void deserialize(char *data, MSG* msgPacket)
                  {
                      int *q = (int*)data;    
                      msgPacket->type = *q;       q++;    
                      msgPacket->priority = *q;   q++;    
                      msgPacket->sender = *q;     q++;
                  
                      char *p = (char*)q;
                      int i = 0;
                      while (i < BUFSIZE)
                      {
                          msgPacket->message[i] = *p;
                          p++;
                          i++;
                      }
                  }
                  
                  void printMsg(MSG* msgPacket)
                  {
                      cout << msgPacket->type << endl;
                      cout << msgPacket->priority << endl;
                      cout << msgPacket->sender << endl;
                      cout << msgPacket->message << endl;
                  }
                  

                  这篇关于结构体的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++中double/float类型的二进制序列化的可移植性 下一篇:c ++从/到二进制文件读/写类

                  相关文章

                  最新文章

                  <i id='Gy7xv'><tr id='Gy7xv'><dt id='Gy7xv'><q id='Gy7xv'><span id='Gy7xv'><b id='Gy7xv'><form id='Gy7xv'><ins id='Gy7xv'></ins><ul id='Gy7xv'></ul><sub id='Gy7xv'></sub></form><legend id='Gy7xv'></legend><bdo id='Gy7xv'><pre id='Gy7xv'><center id='Gy7xv'></center></pre></bdo></b><th id='Gy7xv'></th></span></q></dt></tr></i><div id='Gy7xv'><tfoot id='Gy7xv'></tfoot><dl id='Gy7xv'><fieldset id='Gy7xv'></fieldset></dl></div>

                    <bdo id='Gy7xv'></bdo><ul id='Gy7xv'></ul>

                  1. <legend id='Gy7xv'><style id='Gy7xv'><dir id='Gy7xv'><q id='Gy7xv'></q></dir></style></legend>
                  2. <small id='Gy7xv'></small><noframes id='Gy7xv'>

                    <tfoot id='Gy7xv'></tfoot>