盡管php是用C語言開發(fā)的,不過令我不解的是php沒有提供對結(jié)構(gòu)體struct的直接支持。
不過php提供了pack和unpack函數(shù),用來進(jìn)行二進(jìn)制數(shù)據(jù)(binary data)和php內(nèi)部數(shù)據(jù)的互轉(zhuǎn):
復(fù)制代碼 代碼如下:
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格式類似,有如下一些(中文是我加的,有不準(zhǔn)確的歡迎提出):
a NUL-padded string,即“/0”作為“空字符”的表示形式
A SPACE-padded string,空格作為“空字符”的表示形式
h Hex string, low nibble first,升序位順序
H Hex string, high nibble first,降序位順序
c signed char,有符號單字節(jié)
C unsigned char,無符號單字節(jié)
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,實(shí)際使用的時候作為跳過多少字節(jié)用,很有用
X Back up one byte,后退1字節(jié)
@ NUL-fill to absolute position,實(shí)際使用的時候作為從開頭跳到某字節(jié)用,很有用
實(shí)際使用發(fā)現(xiàn):C里的“/0”(即字符串終止符)在php里并不是終止符,而是作為了字符串的一部分。因此,必須對“/0”進(jìn)行特殊處理,才能進(jìn)行struct和php內(nèi)部數(shù)據(jù)的完美互轉(zhuǎn)。比如 char name[10]; 如果實(shí)際數(shù)據(jù)是“62 69 61 6E 00 62 69 616E00”,在C語言里第5個位置有終止符,name應(yīng)該是“bian”;而用了unpack轉(zhuǎn)換以后在php里的name卻是“bian/0bian/0”。
一開始我用了strpos函數(shù)找到“/0”的位置,然后進(jìn)行substr截取.
不過很Faint的事情發(fā)生了,不知道是strpos的bug還是substr的bug(其實(shí)測試一下就知道,懶得試),有些字符串沒問題,有些字符串卻只能得到空值(即$name == ”)。很是郁悶,后來找了個strtok函數(shù),這下沒有問題了.
難為大家看了那么多,下面寫個完整的php讀取二進(jìn)制數(shù)據(jù)流(C語言結(jié)構(gòu)體struct數(shù)據(jù))文件的示例代碼:
首先是C的struct定義示例,為了演示,我就寫個簡單點(diǎn)的,實(shí)際對照上面那個$format格式表應(yīng)該沒有問題:
復(fù)制代碼 代碼如下:
struct BIANBIAN {
char name[10];
char pass[33];
int age;
unsigned char flag;
};
比如有個“file.dat”文件,內(nèi)容就是上面的N個BIANBIAN結(jié)構(gòu)體構(gòu)成的。讀取的php代碼:
復(fù)制代碼 代碼如下:
<?php
//下面根據(jù)struct確定$format,注意int類型跟機(jī)器環(huán)境有關(guān),我的32位Linux是4個長度
$format = 'a10name/a33pass/iage/Cflag';
//確定一個struct占用多少長度字節(jié),如果只是讀取單個結(jié)構(gòu)體這是不需要的
$length = 10 + 33 + 4 + 1;
//也可以用fopen + fread + fclose,不過file_get_contents因?yàn)榭梢詍map,效率更高
$data = file_get_contents('file.dat', 'r');
for ($i = 0, $c = strlen($data); $i < $c; $i += $length) {
$bianbian = unpack("$format", $data);
//reference傳遞是php 5才支持的,如果用php4,得用其他辦法
foreach ($bianbian as &$value) {
if (is_string($value)) {
$value = strtok($value, "/0");
}
}
print_r($bianbian);
}
?>
pack應(yīng)該跟unpack相反。
順便附上生成結(jié)構(gòu)體文件的C語言代碼:
復(fù)制代碼 代碼如下:
#include <stdio.h>
#include <string.h>
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;
}
php技術(shù):php讀取二進(jìn)制流(C語言結(jié)構(gòu)體struct數(shù)據(jù)文件)的深入解析,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。