php讀取二進(jìn)制流(C語(yǔ)言結(jié)構(gòu)體struct數(shù)據(jù)文件)的深入解析
盡管php是用C語(yǔ)言開(kāi)發(fā)的,不過(guò)令我不解的是php沒(méi)有提供對(duì)結(jié)構(gòu)體struct的直接支持。
不過(guò)php提供了pack和unpack函數(shù),用來(lái)進(jìn)行二進(jìn)制數(shù)據(jù)(binary data)和php內(nèi)部數(shù)據(jù)的互轉(zhuǎn):
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,有符號(hào)單字節(jié)
C unsigned char,無(wú)符號(hào)單字節(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í)際使用的時(shí)候作為跳過(guò)多少字節(jié)用,很有用
X Back up one byte,后退1字節(jié)
@ NUL-fill to absolute position,實(shí)際使用的時(shí)候作為從開(kāi)頭跳到某字節(jié)用,很有用
實(shí)際使用發(fā)現(xiàn):C里的“\0”(即字符串終止符)在php里并不是終止符,而是作為了字符串的一部分。因此,必須對(duì)“\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語(yǔ)言里第5個(gè)位置有終止符,name應(yīng)該是“bian”;而用了unpack轉(zhuǎn)換以后在php里的name卻是“bian\0bian\0”。
一開(kāi)始我用了strpos函數(shù)找到“\0”的位置,然后進(jìn)行substr截取.
不過(guò)很Faint的事情發(fā)生了,不知道是strpos的bug還是substr的bug(其實(shí)測(cè)試一下就知道,懶得試),有些字符串沒(méi)問(wèn)題,有些字符串卻只能得到空值(即$name == ”)。很是郁悶,后來(lái)找了個(gè)strtok函數(shù),這下沒(méi)有問(wèn)題了.
難為大家看了那么多,下面寫(xiě)個(gè)完整的php讀取二進(jìn)制數(shù)據(jù)流(C語(yǔ)言結(jié)構(gòu)體struct數(shù)據(jù))文件的示例代碼:
首先是C的struct定義示例,為了演示,我就寫(xiě)個(gè)簡(jiǎn)單點(diǎn)的,實(shí)際對(duì)照上面那個(gè)$format格式表應(yīng)該沒(méi)有問(wèn)題:
struct BIANBIAN {
char name[10];
char pass[33];
int age;
unsigned char flag;
};
比如有個(gè)“file.dat”文件,內(nèi)容就是上面的N個(gè)BIANBIAN結(jié)構(gòu)體構(gòu)成的。讀取的php代碼:
<?php
//下面根據(jù)struct確定$format,注意int類型跟機(jī)器環(huán)境有關(guān),我的32位Linux是4個(gè)長(zhǎng)度
$format = 'a10name/a33pass/iage/Cflag';
//確定一個(gè)struct占用多少長(zhǎng)度字節(jié),如果只是讀取單個(gè)結(jié)構(gòu)體這是不需要的
$length = 10 + 33 + 4 + 1;
//也可以用fopen + fread + fclose,不過(guò)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語(yǔ)言代碼:
#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;
}
- 深入分析C語(yǔ)言中結(jié)構(gòu)體指針的定義與引用詳解
- 淺談C語(yǔ)言中結(jié)構(gòu)體的初始化
- C語(yǔ)言中結(jié)構(gòu)體(struct)的幾種初始化方法
- C語(yǔ)言 結(jié)構(gòu)體(Struct)詳解及示例代碼
- C語(yǔ)言利用結(jié)構(gòu)體數(shù)組實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
- C語(yǔ)言結(jié)構(gòu)體(struct)常見(jiàn)使用方法(細(xì)節(jié)問(wèn)題)
- C語(yǔ)言 結(jié)構(gòu)體和指針詳解及簡(jiǎn)單示例
- C語(yǔ)言中結(jié)構(gòu)體struct編寫(xiě)的一些要點(diǎn)解析
- 詳解C語(yǔ)言中結(jié)構(gòu)體的自引用和相互引用
- C語(yǔ)言結(jié)構(gòu)體的一些理解
相關(guān)文章
php更新mysql后獲取影響的行數(shù)發(fā)生異常解決方法
mysql_affected_rows函數(shù)當(dāng)UPDATE前后的數(shù)據(jù)一樣時(shí)會(huì)返回異常值,接下來(lái)為大家介紹個(gè)簡(jiǎn)單的解決方法感興趣的朋友可以參考下哈2013-03-03
用PHP實(shí)現(xiàn)Ftp用戶的在線管理的代碼
用PHP實(shí)現(xiàn)Ftp用戶的在線管理的代碼...2007-03-03
學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)訪問(wèn)者模式(Visitor)
這篇文章主要介紹了php設(shè)計(jì)模式中的訪問(wèn)者模式,使用php實(shí)現(xiàn)訪問(wèn)者模式,感興趣的小伙伴們可以參考一下2015-12-12
php+resumablejs實(shí)現(xiàn)的分塊上傳 斷點(diǎn)續(xù)傳功能示例
這篇文章主要介紹了php+resumablejs實(shí)現(xiàn)的分塊上傳 斷點(diǎn)續(xù)傳功能,結(jié)合實(shí)例形式分析了php+resumablejs文件傳輸?shù)木唧w實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
對(duì)比分析php中Cookie與Session的異同
這篇文章主要通過(guò)多方面對(duì)比分析php中Cookie與Session的區(qū)別,分別對(duì)Cookie與Session做一個(gè)簡(jiǎn)單介紹和總結(jié),感興趣的小伙伴們可以參考一下2016-02-02
PHP實(shí)現(xiàn)自動(dòng)對(duì)圖片進(jìn)行滾動(dòng)顯示的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)自動(dòng)對(duì)圖片進(jìn)行滾動(dòng)顯示的方法,涉及php操作圖片特效的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
用PHP ob_start()控制瀏覽器cache、生成html實(shí)現(xiàn)代碼
Output Control 函數(shù)可以讓你自由控制腳本中數(shù)據(jù)的輸出。它非常地有用,特別是對(duì)于:當(dāng)你想在數(shù)據(jù)已經(jīng)輸出后,再輸出文件頭的情況。2010-02-02

