将位字段转换为字节数组

时间:2015-02-26 10:12:53

标签: c arrays struct unions

我有一些mpeg ts位域,例如传输流包:

struct ts_package_header_s {
    unsigned int continuity_counter :4;
    unsigned int adaptation_field_control :2;
    unsigned int transport_scrambling_control :2;
    unsigned int PID :13;
    unsigned int transport_priority :1;
    unsigned int payload_unit_start_indicator :1;
    unsigned int transport_error_indicator :1;
    unsigned int sync_byte :8;
};

struct ts_package_s {
    struct ts_package_header_s ts_header;
    unsigned char ts_body[TS_BODY];
};

union ts_package_u {
    struct ts_package_s ts_package;
    unsigned char bytes[TS_PACKAGE];
};

在我的源代码中,我初始化头文件结构:

pat_package_header.sync_byte = 0x47;
pat_package_header.transport_error_indicator = 0;
pat_package_header.payload_unit_start_indicator = 1;
pat_package_header.transport_priority = 0;
pat_package_header.PID = PAT_PID;
pat_package_header.transport_scrambling_control = 0;
pat_package_header.adaptation_field_control = 1;
pat_package_header.continuity_counter = 0;

而不是我制作ts_packag联盟

union ts_package_u package;
package.ts_package.ts_header = pat_package_header;

比我填充ts_body数组。 当我把这个包写入文件时。我倒退了数组:

10 00 40 47 XX XX XX.. instead of 47 40 10 00 XX XX XX.. 

我试图将我的结构转换为char *而不是使用union,但结果相同。

哪里有错?感谢。

1 个答案:

答案 0 :(得分:3)

将try和serialise这样的结构直接用于磁盘是危险的,其中必须在不同的体系结构中知道格式,或者使用不同的编译器。

编译器存储位域的方式不同,架构的基础字节也会改变数据的存储方式

例如,可能是编译器选择在字节,字或其他边界上对齐位域。这是编译器的决定。它也可以选择以任何顺序保存位,这通常取决于机器的字节顺序。

为了安全地将此标头写入磁盘,您需要自己序列化数据。根据{{​​3}}

,标题为32位和Big Endian

例如:

#include <stdio.h>

#define TS_BODY 1024
#define PAT_PID 0x40

struct ts_package_header_s {
    unsigned int continuity_counter :4;
    unsigned int adaptation_field_control :2;
    unsigned int transport_scrambling_control :2;
    unsigned int PID :13;
    unsigned int transport_priority :1;
    unsigned int payload_unit_start_indicator :1;
    unsigned int transport_error_indicator :1;
    unsigned int sync_byte :8;
};

struct ts_package_s {
    struct ts_package_header_s ts_header;
    unsigned char ts_body[TS_BODY];
};

static void write_ts( struct ts_package_s pat_package )
{
    FILE* f = fopen( "test.ts", "wb+" );
    unsigned int header = 0;

    if( f == NULL )
        return;

    header = pat_package.ts_header.sync_byte << 24;
    header |= ( pat_package.ts_header.transport_error_indicator << 23 );
    header |= ( pat_package.ts_header.payload_unit_start_indicator << 22 );
    header |= ( pat_package.ts_header.transport_priority << 21 );
    header |= ( pat_package.ts_header.PID << 8 );
    header |= ( pat_package.ts_header.transport_scrambling_control << 6 );
    header |= ( pat_package.ts_header.adaptation_field_control << 4 );
    header |= ( pat_package.ts_header.continuity_counter );

    /* Write the 32-bit header as big-endian */
    unsigned char byte = header >> 24;
    fwrite( &byte, 1, 1, f );

    byte = ( header >> 16 ) & 0xFF;
    fwrite( &byte, 1, 1, f );

    byte = ( header >> 8 ) & 0xFF;
    fwrite( &byte, 1, 1, f );

    byte = header & 0xFF;
    fwrite( &byte, 1, 1, f );

    fclose( f );
}

int main( int argc, char* argv[] )
{
    struct ts_package_s pat_package;
    pat_package.ts_header.sync_byte = 0x47;
    pat_package.ts_header.transport_error_indicator = 0;
    pat_package.ts_header.payload_unit_start_indicator = 1;
    pat_package.ts_header.transport_priority = 0;
    pat_package.ts_header.PID = PAT_PID;
    pat_package.ts_header.transport_scrambling_control = 0;
    pat_package.ts_header.adaptation_field_control = 1;
    pat_package.ts_header.continuity_counter = 0;

    write_ts( pat_package );

    return 0;
}

使用以下标题写入文件:

0x47 0x40 0x01 0x10
根据您使用的值,

似乎是正确的。