Use PHP to operate binary files. (If you have any binary operation experience, come in and give me some answers. Thank you)

Source: Internet
Author: User
Tags unpack

Binary is a very biased thing for PHP, because PHP has very limited support for binary, I discussed a problem in a PHP Group today and did not discuss the results in the afternoon. Now I want to summarize it, I thought I understood it quite well. Later I had a lot of discussions, and I was confused about binary.

Q: A friend in the Group accepts a project, which was previously written in Java. There are many operations on binary files, including using images as binary storage, then, add some binary formats of user input strings to the binary file, that is, combine the image and string into a binary file, we are stuck in the question of how to write a string into a binary file.

1 <? PHP
2 $ Header_str = " ABC123 " ;
3
4 Function Asc2bin ( $ Temp ){
5 $ Len   =   Strlen ( $ Temp );
6 For ( $ I = 0 ; $ I < $ Len ; $ I ++ ) $ Data . = Sprintf ( " % 02x " , ORD ( Substr ( $ Temp , $ I , 1 )));
7 Return   $ Data ;
8 }
9
10 $ AA   = Asc2bin ( $ Header_str );
11
12
13 $ Content_2 = Pack ( ' H * ' ,   $ AA );
14
15 $ Handle   =   Fopen ( " Aaaa. Bin " ,   " WB " );
16 Fwrite ( $ Handle ,   $ Content_2 );
17 Fclose ( $ Handle );
18
19 ?>

Below is my finalCodeIt writes a string of ABC123 into the binary file AAAA as a binary stream. bin, which uses the pack function. This function can convert the built-in PHP type into binary format. It is compatible with the Unpack function. The latter can convert binary to the built-in PHP type.

The two are used as follows:

Code
1 1   String   Pack ( String   $ Format [ ,   Mixed   $ ARGs [ ,   Mixed $ ])
2 2   // Pack given arguments into binary string according to format.
3 3
4 4   Array   Unpack ( String   $ Format ,   String   $ Data )
5 5   // Unpacks from a binary string into an array according to the given format.
6 Where, $ Format is similar to pack format in Perl. , There are some of the following (I added the Chinese language and are welcome to raise it if it is inaccurate ):
7 A NUL - Padded String , That is, "\ 0 "As the expression of" null character"
8 A space - Padded String , Space as the expression of "null character"
9 H hex String , Low nibble first, ascending order
10 H hex String , High nibble first, descending order
11 C signed Char, signed single-byte
12 C unsigned char, unsigned single-byte
13 S signed short (always 16 Bit , Machine byte order)
14 S unsigned short (always 16 Bit , Machine byte order)
15 N unsigned short (always 16 Bit , Big endian byte order)
16 V unsigned short (always 16 Bit , Little endian byte order)
17 I signed Integer (Machine dependent size and byte order)
18 I unsigned Integer (Machine dependent size and byte order)
19 L signed long (always 32 Bit , Machine byte order)
20 L unsigned long (always 32 Bit , Machine byte order)
21 N unsigned long (always 32 Bit , Big endian byte order)
22 V unsigned long (always 32 Bit , Little endian byte order)
23 F Float (Machine dependent size and representation)
24 D Double (Machine dependent size and representation)
25 X nul byte, which is useful when used as the number of bytes skipped
26 X back up one byte, 1 byte back
27 @ NUL -

The above is the usage, but we encountered a problem in our discussion. In the final analysis, does it mean that the binary file may be displayed in notepad in a non-garbled form? Many people in the group said that the binary files should be garbled in the notepad. At first, I was convinced that the binary files were corrupted.ProgramI have no confidence, because although this binary file is correctly displayed in the hexadecimal format in advanced text processing tools, it does not display garbled characters in notepad, "ABC123" is displayed. I thought I should make a mistake. I should write the string directly into the binary file instead of the binary stream.

Now I came back and thought about it. Is it possible that the binary file is garbled? English letters are encoded in bytes. An English letter occupies one byte and eight digits. After I write a binary string into a binary file, the text will certainly be displayed in notepad, because notepad automatically parses binary data and converts it to the string format, I think the above program is actually correct.

In fact, I am not sure that binary files are a complicated concept. Different binary files have different structures and have little knowledge about binary, no one in the group can clearly understand what a binary file is, and how to write a string to a binary file should not be mentioned.

Here. there are a lot of people in. net. Everyone should have a concept of binary. I am eager to ask experts, but it is still wrong if I write binary files like this. I don't ask the code, but after writing the string in binary format to a file, whether the display is garbled or the string itself.

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.