C++ 将结构体转换为字节并返回结构体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13775893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:42:24  来源:igfitidea点击:

Converting struct to byte and back to struct

c++structbytearduinoxbee

提问by Steven10172

I'm currently working with Arduino Unos, 9DOFs, and XBees, and I was trying to create a struct that could be sent over serial, byte by byte, and then re-constructed into a struct.

我目前正在使用Arduino Unos、9DOFs 和XBees,我试图创建一个可以通过串行、逐字节发送的结构,然后重新构造成一个结构。

So far I have the following code:

到目前为止,我有以下代码:

struct AMG_ANGLES {
    float yaw;
    float pitch;
    float roll;
};

int main() {
    AMG_ANGLES struct_data;

    struct_data.yaw = 87.96;
    struct_data.pitch = -114.58;
    struct_data.roll = 100.50;

    char* data = new char[sizeof(struct_data)];

    for(unsigned int i = 0; i<sizeof(struct_data); i++){
        // cout << (char*)(&struct_data+i) << endl;
        data[i] = (char*)(&struct_data+i); //Store the bytes of the struct to an array.
    }

    AMG_ANGLES* tmp = (AMG_ANGLES*)data; //Re-make the struct
    cout << tmp.yaw; //Display the yaw to see if it's correct.
}

Source: http://codepad.org/xMgxGY9Q

来源:http: //codepad.org/xMgxGY9Q

This code doesn't seem to work, and I'm not sure what I'm doing wrong.

这段代码似乎不起作用,我不确定我做错了什么。

How do I solve this?

我该如何解决这个问题?

回答by Steven10172

It seems I've solved my issue with the following code.

看来我已经用下面的代码解决了我的问题。

struct AMG_ANGLES {
    float yaw;
    float pitch;
    float roll;
};

int main() {
    AMG_ANGLES struct_data;

    struct_data.yaw = 87.96;
    struct_data.pitch = -114.58;
    struct_data.roll = 100.50;

    //Sending Side
    char b[sizeof(struct_data)];
    memcpy(b, &struct_data, sizeof(struct_data));

    //Receiving Side
    AMG_ANGLES tmp; //Re-make the struct
    memcpy(&tmp, b, sizeof(tmp));
    cout << tmp.yaw; //Display the yaw to see if it's correct
}

WARNING: This code will only work if sending and receiving are using the same endianarchitecture.

警告:此代码仅在发送和接收使用相同的字节序架构时才有效。

回答by Some programmer dude

You do things in the wrong order, the expression

你以错误的顺序做事,表达

&struct_data+i

takes the address of struct_dataand increases it by itimes the size of the structure.

获取 的地址struct_data并将其增加i为结构大小的倍数

Try this instead:

试试这个:

*((char *) &struct_data + i)

This converts the address of struct_datato a char *and thenadds the index, and then uses the dereference operator (unary *) to get the "char" at that address.

这会将 的地址转换struct_data为 a char *然后添加索引,然后使用解引用运算符 (unary *) 获取该地址处的“char”。

回答by CharLess

Always utilize data structures to its fullest..

始终充分利用数据结构。

union AMG_ANGLES {
  struct {
    float yaw;
    float pitch;
    float roll;
  }data;
  char  size8[3*8];
  int   size32[3*4];
  float size64[3*1];
};

回答by jtepe

for(unsigned int i = 0; i<sizeof(struct_data); i++){
    // +i has to be outside of the parentheses in order to increment the address
    // by the size of a char. Otherwise you would increment by the size of
    // struct_data. You also have to dereference the whole thing, or you will
    // assign an address to data[i]
    data[i] = *((char*)(&struct_data) + i); 
}

AMG_ANGLES* tmp = (AMG_ANGLES*)data; //Re-Make the struct
//tmp is a pointer so you have to use -> which is shorthand for (*tmp).yaw
cout << tmp->yaw; 
}