我有一些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;

比我做包装联合会
union ts_package_u package;
package.ts_package.ts_header = pat_package_header;

比我填满尸体阵列。
当我把这个包写入文件时。我倒排:
10 00 40 47 XX XX XX.. instead of 47 40 10 00 XX XX XX..

我尝试将结构转换为char*而不是使用union,但得到了相同的结果。
哪里有错?谢谢。

最佳答案

使用try和serialise这样的结构直接到磁盘是很危险的,在磁盘上必须知道不同体系结构的格式,或者使用不同的编译器。
编译器在存储位字段的方式上有所不同,而且体系结构的底层端也会更改数据的存储方式
例如,编译器可能选择在字节、字或其他边界上对齐位字段。这是编译器的决定。它也可以选择以任何顺序保存位,这通常取决于机器的端号。
为了安全地将这个头写入磁盘,您需要自己序列化数据。根据Wikipedia.
例如:

#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

根据你正在使用的值,这似乎是正确的。

关于c - 将位字段转换为字节数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28739622/

10-11 21:19