In-depth parsing of PHP read binary stream (c language struct struct data file) _php Tutorial

Source: Internet
Author: User
Tags fread rewind strtok unpack
Although PHP was developed in C, I was puzzled that PHP did not provide direct support for struct structs.
However, PHP provides the pack and unpack functions for the reciprocal transfer of binary data and PHP internal data:
Copy CodeThe code is 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 and Perl in the pack format similar, there are some (I added, there is an inaccurate welcome to the proposed):
A nul-padded string, that is, "\" as a representation of the "null character"
A space-padded string, space as a representation of "null character"
H Hex string, low nibble first, ascending order
H Hex string, high nibble first, descending bit order
C signed Char, signed single byte
C unsigned char, unsigned single byte
s signed short (always the bit, machine byte order)
S unsigned short (always the bit, machine byte order)
n unsigned short (always-bit, big endian byte order)
V unsigned short (always-on 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 the bit, machine byte order)
L unsigned long (always the bit, machine byte order)
N unsigned long (always the bit, big endian byte order)
V unsigned long (always the bit, little endian byte order)
f Float (machine dependent size and representation)
D double (machine dependent size and representation)
x NUL byte, useful when actually used as a skipped number of bytes
X back up one byte, backward 1 bytes
@ Nul-fill to absolute position, useful when actually used as jumping from the beginning to a byte
The actual use found that the "\" in C (that is, the string terminator) in PHP is not a terminator, but as part of the string. Therefore, it is necessary to do special processing of "", in order to perform the perfect mutual transfer between the struct and PHP internal data. such as Char name[10]; If the actual data is "6E 616E00", in C language 5th position has the Terminator, the name should be "Bian", and after the unpack conversion in PHP name is "Bian\0bian\0".
At first I used the Strpos function to find the location of "M" and then substr intercept.

But very faint things happen, do not know is strpos bug or substr bug (in fact, test to know, too lazy to try), some strings are OK, some strings can only get null value (that is, $name = = "). Very depressed, and later found a strtok function, this is no problem.
Hard to see so many, the following write a complete PHP read binary data stream (c language struct struct data) file Sample code:
The first is the struct definition example of C, in order to demonstrate, I will write a simple point, the actual control of the above $format format table should be no problem:
Copy CodeThe 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 is the above n Bianbian structure. PHP code to read:
Copy CodeThe code is as follows:
The following determines the $format based on the struct, noting that the int type is related to the machine environment, my 32-bit Linux is 4 lengths
$format = ' A10name/a33pass/iage/cflag ';
Determines how many length bytes a struct occupies, which is not required if it is just reading a single struct
$length = 10 + 33 + 4 + 1;
can also use fopen + Fread + fclose, but file_get_contents because can mmap, more efficient
$data = file_get_contents (' file.dat ', ' R ');
for ($i = 0, $c = strlen ($data), $i < $c, $i + = $length) {
$bianbian = Unpack ("$format", $data);
Reference delivery is supported by PHP 5, if you use PHP4, you have to use other methods
foreach ($bianbian as & $value) {
if (is_string ($value)) {
$value = Strtok ($value, "n");
}
}
Print_r ($bianbian);
}
?>

Pack should be the opposite of unpack.
By the way, enclose the C language code that generated the struct file:
Copy CodeThe code is as follows:
#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.html www.bkjia.com true http://www.bkjia.com/PHPjc/327566.html techarticle Although PHP was developed in C, I was puzzled that PHP did not provide direct support for struct structs. However, PHP provides the pack and unpack functions, which are used for binary data (... ).

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.