c语言如何给位域变量赋值,C语言的位域赋值问题

系统:ubuntu 11.04  little-end

描述:这个问题是在研究TS包头解析时遇到的,用两种方式取TS包头,但结果不同

耗时:2天

程序:

1. 使用正常顺序方式定义TS包头结构体,然后将TS包头数据拷贝到包头结构体中。

void bob_get_packet_header_mem_cpy_1(unsigned char *ts_package)

{

printf(" [%s]:\n", __func__);

printf(" Package Source Data: ");

unsigned char *p = ts_package;

while(p < (ts_package + sizeof(TS_packet_header))){

printf("%02x ", *p);

p++;

}

printf(" (0x%04x)\n", *(unsigned int *)ts_package);

struct bob_TS_packet_header

{

unsigned int sync_byte :8;

unsigned int transport_error_indicator :1;

unsigned int payload_unit_start_indicator :1;

unsigned int transport_priority :1;

unsigned int PID :13;

unsigned int transport_scrambling_control :2;

unsigned int adaptation_field_control :2;

unsigned int continuity_counter :4;

}header;

memcpy((void *)&header, ts_package, sizeof(header));

printf(" sync_byte(8b): 0x%02X (%u) %s\n", header.sync_byte, header.sync_byte, (header.sync_byte == 0x47 ? "" : "(sync error)"));

printf(" transport_error_indicator(1b): 0x%02X (%u) %s\n", header.transport_error_indicator, header.transport_error_indicator, (header.transport_error_indicator == 0x00 ? "" : "(packet error)"));

printf(" payload_unit_start_indicator(1b): 0x%02X (%u)\n", header.payload_unit_start_indicator, header.payload_unit_start_indicator);

printf(" transport_priority(1b): 0x%02X (%u)\n", header.transport_priority, header.transport_priority);

printf(" PID(13b): 0x%04X (%u)\n", header.PID, header.PID);

printf(" transport_scrambling_control(2b): 0x%02X (%u) %s\n", header.transport_scrambling_control, header.transport_scrambling_control

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值