Home >Backend Development >PHP Tutorial >In-depth analysis of PHP reading binary stream (C language structure struct data file)_PHP tutorial

In-depth analysis of PHP reading binary stream (C language structure struct data file)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:07:101303browse

Although php is developed in C language, what puzzles me is that php does not provide direct support for the structure struct.
However, PHP provides pack and unpack functions for converting binary data and PHP internal data:

Copy code Code As follows:

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.

Among them, $format is similar to the pack format in perl , there are some as follows (Chinese is added by me, any inaccuracies are welcome):
a NUL-padded string, that is, "
But something very Faint happened. I don’t know if it is a strpos bug or a substr bug (in fact, you will know after testing it, but I am too lazy to try). Some strings are fine, but some strings can only get null values ​​(i.e. $ name == "). I was very depressed, and then I found a strtok function, and now there is no problem.
It’s hard for everyone to read so much, so let’s write a complete PHP to read the binary data stream (C language structure struct Data) file sample code:
First is the C struct definition example. For demonstration, I will write a simpler one. In fact, there should be no problem if you compare it with the $format format table above:



Copy code

The code is as follows:


struct BIANBIAN {
char name[10];
char pass[33];
int age;
unsigned char flag;
};


For example, there is a "file.dat" file, the content of which is composed of the N BIANBIAN structures above:

Copy code

The code is as follows:


//Determine $format based on struct below. Note that the int type is related to the machine environment. My 32-bit Linux is 4 lengths
$format = 'a10name/a33pass/ iage/Cflag';
//Determine how many bytes of length a struct occupies. This is not needed if you just read a single structure.
$length = 10 + 33 + 4 + 1;
// You can also use fopen + fread + fclose, but file_get_contents is more efficient because it can be mmap
$data = file_get_contents('file.dat', 'r');
for ($i = 0, $c = strlen($data); $i < $c; $i += $length) {
$bianbian = unpack("$format", $data);
//reference passing is only supported by PHP 5 Yes, if you use php4, you have to use other methods
foreach ($bianbian as &$value) {
if (is_string($value)) {
$value = strtok($value, "



http://www.bkjia.com/PHPjc/327566.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/327566.html
TechArticleAlthough php is developed in C language, what puzzles me is that php does not provide support for the structure struct Direct support. However, PHP provides pack and unpack functions for binary data (...



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn