国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
php讀取二進(jìn)制流(C語言結(jié)構(gòu)體struct數(shù)據(jù)文件)
轉(zhuǎn)載來自http://bianbian.org/
盡管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):
  1. string pack ( string $format [, mixed $args [, mixed $...]] )
  2. //Pack given arguments into binary string according to format.
  3. array unpack ( string $format, string $data )
  4. //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,實際使用的時候作為跳過多少字節(jié)用,很有用
X Back up one byte,后退1字節(jié)
@ NUL-fill to absolute position,實際使用的時候作為從開頭跳到某字節(jié)用,很有用

實際使用發(fā)現(xiàn):C里的“\0”(即字符串終止符)在php里并不是終止符,而是作為了字符串的一部分。因此,必須對“\0”進(jìn)行特殊處理,才能進(jìn)行struct和php內(nèi)部數(shù)據(jù)的完美互轉(zhuǎn)。比如 char name[10]; 如果實際數(shù)據(jù)是“62 69 61 6E 00 62 69 61 6E 00”,在C語言里第5個位置有終止符,name應(yīng)該是“bian”;而用了unpack轉(zhuǎn)換以后在php里的name卻是“bian\0bian\0”。
一開始我用了strpos函數(shù)找到“\0”的位置,然后進(jìn)行substr截?。?/p>

  1. $name = substr($name, 0, strpos($name, "\0"));

不過很Faint的事情發(fā)生了,不知道是strpos的bug還是substr的bug(其實測試一下就知道,懶得試),有些字符串沒問題,有些字符串卻只能得到空值(即$name == ”)。很是郁悶,后來找了個strtok函數(shù),這下沒有問題了:

  1. $name = strtok($name, "\0");

難為大家看了那么多,下面寫個完整的php讀取二進(jìn)制數(shù)據(jù)流(C語言結(jié)構(gòu)體struct數(shù)據(jù))文件的示例代碼:
首先是C的struct定義示例,為了演示,我就寫個簡單點的,實際對照上面那個$format格式表應(yīng)該沒有問題:

  1. struct BIANBIAN {
  2.     char name[10];
  3.     char pass[33];
  4.     int  age;
  5.     unsigned char flag;
  6. };

比如有個“bianbian.org”文件,內(nèi)容就是上面的N個BIANBIAN結(jié)構(gòu)體構(gòu)成的。讀取的php代碼:

  1. //下面根據(jù)struct確定$format,注意int類型跟機(jī)器環(huán)境有關(guān),我的32位Linux是4個長度
  2. $format = 'a10name/a33pass/iage/Cflag';
  3. //確定一個struct占用多少長度字節(jié),如果只是讀取單個結(jié)構(gòu)體這是不需要的
  4. $length = 10 + 33 + 4 + 1;
  5. //也可以用fopen + fread + fclose,不過file_get_contents因為可以mmap,效率更高
  6. $data = file_get_contents('bianbian.org', 'r');
  7. for ($i = 0, $c = strlen($data); $i < $c; $i += $length) {
  8.     $bianbian = unpack("@$i/$format", $data);
  9.     //reference傳遞是php 5才支持的,如果用php4,得用其他辦法
  10.     foreach ($bianbian as &$value) {
  11.         if (is_string($value)) {
  12.             $value = strtok($value, "\0");
  13.         }
  14.     }
  15.     print_r($bianbian);
  16. }
  17. //輸出為array,即類似:
  18. Array
  19. (
  20.     [name] => 'bianbian'
  21.     [pass] => 'bianbian.org'
  22.     [age]  => 100
  23.     [flag] => 0
  24. )
  25. ...

pack應(yīng)該跟unpack相反。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Lua struct
Python使用struct處理二進(jìn)制
python讀取mnist
perl pack和unpack的使用方法
Python模塊學(xué)習(xí)----struct數(shù)據(jù)格式轉(zhuǎn)換
Python字節(jié)流打包拆包
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服