简体   繁体   English

如何在C中将struct转换为char数组?

[英]How to convert struct to char array in C?

I am trying to send an ethernet packet using RAW socket in C Linux. 我正在尝试使用C Linux中的RAW套接字发送以太网数据包。 I have following struct definition in my code: 我的代码中有以下struct定义:

typedef struct vlink_header_s
{
    uint8_t verCmd;     
    uint8_t reverseVerCmd;
}vlink_header_t;

typedef struct vlink_reg_rd_s
{
    vlink_header_t  header;
    uint32_t        address;
    uint16_t        length;
}vlink_reg_rd_t;

In main i created a struct: main创建了一个结构:

vlink_reg_rd_t g_pkt;
g_pkt.header.verCmd = 0x10|VLINK_CMD_REG_RD;
g_pkt.header.reverseVerCmd = ~(g_pkt.header.verCmd);
g_pkt.address = 0x0007 ..... 

and message: 和消息:

char sendbuf[1024];
struct ether_header *eh = (struct ether_header *) sendbuf;

how do I add all the info from the struct g_pkt to this sendbuf after ether_header so I can send a complete packet using: 我如何将所有结构体g_pkt中的信息添加到ether_header之后的sendbuf中,以便使用以下命令发送完整的数据包:

sendto(sockfd, sendbuf, txLen, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll));

Everything else in my code is working, I tried other ways by adding info to sendbuf one by one and it works fine and my machine receive the packets on the other side too. 我代码中的所有其他内容都在工作,我尝试了其他方法,将信息一个接一个地添加到sendbuf ,并且工作正常,我的机器也从另一侧接收数据包。 I just want to make it more versatile because there are a bunch of commands and structs for each commands will work best. 我只是想使其更加通用,因为有很多命令,每个命令的结构都可以最好地发挥作用。 Thanks. 谢谢。

Try to use the memcpy function: 尝试使用memcpy函数:

#include<iostream>

typedef struct vlink_header_s
{
    uint8_t verCmd;
    uint8_t reverseVerCmd;
}vlink_header_t;

typedef struct vlink_reg_rd_s
{
    vlink_header_s  header;
    uint32_t        address;
    uint16_t        length;
}vlink_reg_rd_t;

using namespace std;
int main()
{
    vlink_reg_rd_t data;

    //TODO: Set values into typedef data.

    int size = sizeof(vlink_reg_rd_t); //get 

    char* buffer = new char[size];
    memset(buffer, 0x00, size);

    memcpy(buffer, &data, size); //Copy data from vlink_reg_rd_t to char*

    //TODO: Send the buffer.

    delete[] buffer; //free memory

    return 0;
}

IMPORTANT : be aware of the order in which data types number are written into the buffer. 重要说明 :请注意将数据类型编号写入缓冲区的顺序。 Also it is necessary check the align the data into the structure to avoid extra bytes at the moment of use memcpy . 另外,有必要检查将数据对齐到结构中,以避免在使用memcpy多余的字节。 Here you can check this topic: 在这里您可以检查此主题:

for Microsoft: 对于Microsoft:

https://msdn.microsoft.com/en-us/library/xh3e3fd0.aspx https://msdn.microsoft.com/en-us/library/83ythb65.aspx https://msdn.microsoft.com/zh-CN/library/xh3e3fd0.aspx https://msdn.microsoft.com/zh-CN/library/83ythb65.aspx

For Gcc: 对于Gcc:

https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Type-Attributes.html https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Type-Attributes.html

I have done this way with protocol buffer , you can take a look at : https://www.google.com.vn/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=protocol+buffer& * . 我已经使用协议缓冲区完成了此操作,可以看看: https : //www.google.com.vn/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q= protocol+buffer & * 。

Edit : this way called serialize data , as you serialize your data into a proto file then compile it to the packet file that you use on both server and client . 编辑:这种方式称为序列化数据,当您将数据序列化为原型文件,然后将其编译为在服务器和客户端上使用的数据包文件时。

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

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