您好,欢迎访问一九零五行业门户网

php读取二进制流(C语言结构体struct数据文件)的深入解析_PHP教程

尽管php是用c语言开发的,不过令我不解的是php没有提供对结构体struct的直接支持。
不过php提供了pack和unpack函数,用来进行二进制数据(binary data)和php内部数据的互转:
复制代码 代码如下:
string pack ( string $format [, mixed $args [, mixed $...]] ) 
 //pack given arguments into binary string according to format. 
array unpack ( string $format, string $data ) 
//unpacks from a binary string into an array according to the given format.
其中,$format跟perl里的pack格式类似,有如下一些(中文是我加的,有不准确的欢迎提出):
a nul-padded string,即“\0”作为“空字符”的表示形式
a space-padded string,空格作为“空字符”的表示形式
h hex string, low nibble first,升序位顺序
h hex string, high nibble first,降序位顺序
c signed char,有符号单字节
c unsigned char,无符号单字节
s signed short (always 16 bit, machine byte order)
s unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
i unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
l unsigned long (always 32 bit, machine byte order)
n unsigned long (always 32 bit, big endian byte order)
v unsigned long (always 32 bit, little endian byte order)
f float (machine dependent size and representation)
d double (machine dependent size and representation)
x nul byte,实际使用的时候作为跳过多少字节用,很有用
x back up one byte,后退1字节
@ nul-fill to absolute position,实际使用的时候作为从开头跳到某字节用,很有用
实际使用发现:c里的“\0”(即字符串终止符)在php里并不是终止符,而是作为了字符串的一部分。因此,必须对“\0”进行特殊处理,才能进行struct和php内部数据的完美互转。比如 char name[10]; 如果实际数据是“62 69 61 6e 00 62 69 616e00”,在c语言里第5个位置有终止符,name应该是“bian”;而用了unpack转换以后在php里的name却是“bian\0bian\0”。
一开始我用了strpos函数找到“\0”的位置,然后进行substr截取.不过很faint的事情发生了,不知道是strpos的bug还是substr的bug(其实测试一下就知道,懒得试),有些字符串没问题,有些字符串却只能得到空值(即$name == ”)。很是郁闷,后来找了个strtok函数,这下没有问题了.
难为大家看了那么多,下面写个完整的php读取二进制数据流(c语言结构体struct数据)文件的示例代码:
首先是c的struct定义示例,为了演示,我就写个简单点的,实际对照上面那个$format格式表应该没有问题:
复制代码 代码如下:
struct bianbian { 
    char name[10]; 
    char pass[33]; 
    int  age; 
    unsigned char flag; 
};
比如有个“file.dat”文件,内容就是上面的n个bianbian结构体构成的。读取的php代码:
复制代码 代码如下:
pack应该跟unpack相反。
顺便附上生成结构体文件的c语言代码:
复制代码 代码如下:
#include  
    #include
struct example      
    {     
        char name[10]; 
        char pass[33]; 
        int  age; 
        unsigned char flag; 
    };
int main()    
    { 
        example test; 
        example read;    
        file *fp;
test.age = 111;    
        test.flag = 10; 
        strcpy(test.name, hello world!); 
        strcpy(test.pass, zbl110119);
fp = fopen(file.dat, w+); 
        if (!fp) 
        { 
            printf(open file error!); 
            return -1; 
        }
rewind(fp); 
        fwrite(&test, sizeof(example), 1, fp);
rewind(fp); 
        fread(&read, sizeof(example), 1, fp);
printf(%d, %s\n, read.age, read.name);
fclose(fp); 
        return 0; 
    }
http://www.bkjia.com/phpjc/327566.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/327566.htmltecharticle尽管php是用c语言开发的,不过令我不解的是php没有提供对结构体struct的直接支持。 不过php提供了pack和unpack函数,用来进行二进制数据(...
其它类似信息

推荐信息