简体   繁体   English

用C编码和解码数据

[英]encode and decode data in C

I have to write a C program that encodes data and stores it into a buffer to later perform some operation on that buffer. 我必须编写一个对数据进行编码并将其存储到缓冲区中的C程序,以便以后对该缓冲区执行一些操作。 I have the following structures: 我有以下结构:

typedef uint64_t time64;
typedef int32_t sint32;

typedef struct
{
    sint32 latitude;
    sint32 longitude;
    int16_t elevation; 
}ThreeDLocation;

typedef struct
{
    uint8_t permission_indices;
    char* data;
    time64 expiry_time;
    ThreeDLocation generation_location;
}data;

I want to encode/decode the data structure into a single buffer. 我想将data结构编码/解码到单个缓冲区中。 I think this is called “serialize” and “deserialize”. 我认为这称为“序列化”和“反序列化”。

Can anyone give me some hints on how to solve this? 谁能给我一些如何解决这个问题的提示?

You could try to just point an integer to the struct and read it out that way: 您可以尝试将一个整数指向该结构并以这种方式读出:

data blockdata;
int* serialdata;
int i;
int n = sizeof(data);
filldata(&blockdata);
serialdata = (int*)&blockdata;

for(i = 0; i < n; i++)
{
   globalbuffer[index++] = serialdata[i];
}

Not sure if this helps 不确定这是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM