c程序打印联合体union的字节

#include <stdio.h>
#include <stdbool.h>

typedef struct 
{
    union  
    {
        unsigned char PORT_CC1;  
        struct
        {
        unsigned char CC1_CMPO:1;
        unsigned char CC1_RD:1;
        unsigned char reverse1:6;
        };
    };   
    union  
    {
        unsigned char PORT_CC3;  
        struct
        {
        unsigned char CC3_CMPO:1;
        unsigned char CC3_RD:1;
        unsigned char reverse3:6;
        };
    };
}DATA;



void main()
{
    DATA test_data;
    printf("struct len=%d\n",sizeof(test_data)/sizeof(char));
    test_data.PORT_CC1=0x00;
    test_data.PORT_CC3=0x00;

    test_data.reverse1=0b111111;
    test_data.reverse3=0b101010;
    printf("test_data.PORT_CC1=%02x  test_data.PORT_CC3=%02x\n",test_data.PORT_CC1,test_data.PORT_CC3);
    test_data.CC1_CMPO=1;
    test_data.CC1_RD=0;
    printf("test_data.PORT_CC1=%02x  test_data.PORT_CC3=%02x\n",test_data.PORT_CC1,test_data.PORT_CC3);
    test_data.PORT_CC1=0xfc;
    printf("test_data.PORT_CC1=%02x  test_data.PORT_CC3=%02x\n",test_data.PORT_CC1,test_data.PORT_CC3);
    printf("test_data.CC1_CMPO=%d test_data.CC1_RD=%d\n",test_data.CC1_CMPO,test_data.CC1_RD);
}

#include<stdio.h>

union myUnion {  

    uint16_t msg_type:                      5;          // [4:0]:   Message Type

    uint16_t port_data_role_or_reserved:    1;          // [5]:     Port Data Role or Reserved

    uint16_t specification_revision:        2;          // [7:6]:   Specification revision

    uint16_t port_power_role_or_cable_plug: 1;          // [8]:     Port Power Role or Cable Plug

    uint16_t msg_id:                        3;          // [11:9]:  Message ID

    uint16_t data_objects_cnt:              3;          // [14:12]: Number of Data Objects

    uint16_t extended:                      1;          // [15]:    Extended

    //float f;

    uint16_t i;

};

struct myStruct {

    union myUnion data;

    uint32_t id;

};

void printBytes(void* data, size_t size) {

    unsigned char* bytes = (unsigned char*)data;

    for (size_t i = 0; i < size; i++) {

        printf("%02x ", bytes[i]);

    }

    printf("\n");

}

int main() {

    struct myStruct info;

    info.data.i =0x88ff;

    info.id =0x12345678;

    myDebug("msg_type=%02x\n", info.data.msg_type);

    myDebug("port_data_role_or_reserved=%02x\n", info.data.port_data_role_or_reserved);

    myDebug("specification_revision=%02x\n", info.data.specification_revision);

    myDebug("port_power_role_or_cable_plug=%02x\n", info.data.port_power_role_or_cable_plug);

    myDebug("msg_id=%02x\n", info.data.msg_id);

    myDebug("data_objects_cnt=%02x \n", info.data.data_objects_cnt);

    myDebug("extended=%02x \n", info.data.extended);

    printf("Byte data of union:len=%d ",sizeof(union myUnion));

    printBytes(&info.data, sizeof(union myUnion));

    printf("Byte data of struct:len=%d ",sizeof(struct myStruct));

    printBytes(&info, sizeof(struct myStruct));

    return 0;

}

注意联合体内的成员共享同一段内存,因此不能同时赋值。否则其他变量值不确定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值