Use php to operate binary files

Source: Internet
Author: User
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. problem: there are

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. problem: there are

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 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?>

The following is my final code. It writes an abc123 string 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

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 say that viewing binary files in Notepad should be garbled. At first, I was also convinced that binaries were lost, so I had no confidence in my own programs, although I checked this binary file in advanced text processing tools, the hexadecimal format is indeed correct, but it does not show garbled characters in notepad, but shows "abc123 ", I started to think I should be wrong. I should write the string directly into the binary file, rather than 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.

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.