Use php to write the binary form of a file

Source: Internet
Author: User
Xxdfilename can display the binary form of a file on the terminal without considering the encoding of the file. How to use php to implement the same function? 1. fopen2.fread3. ehco does not work. it cannot display the binary value of the text. The text is displayed. how can this problem be solved? I wrote a paragraph... xxd filename
You can display the binary form of a file on the terminal without considering the encoding of the file.
How to use php to implement the same function?
1. fopen
2. fread
3. ehco does not work. The binary text cannot be displayed. The text is displayed. how can this problem be solved?

I wrote a piece of code


  

No. the character itself is displayed, not the binary expression of the character.
This ehco is expected to be modified. how can this problem be modified.
Fwrite (STDOUT, $ byte); replace echo $ byte; neither.

The modification is not far from success.


  Test xxd-B./test.txt 0000000: 00001010 using a file containing only the line feed key. the program running result is: 10100.

Reply content:

Xxd filename
You can display the binary form of a file on the terminal without considering the encoding of the file.
How to use php to implement the same function?
1. fopen
2. fread
3. ehco does not work. The binary text cannot be displayed. The text is displayed. how can this problem be solved?

I wrote a piece of code


  

No. the character itself is displayed, not the binary expression of the character.
This ehco is expected to be modified. how can this problem be modified.
Fwrite (STDOUT, $ byte); replace echo $ byte; neither.

The modification is not far from success.


  Test xxd-B./test.txt 0000000: 00001010 using a file containing only the line feed key. the program running result is: 10100.

Change the basic mode of the fopen mode parameter to B, as shown in figurerbTo distinguish between binary streams and text files.

You can also use 'B' to force the binary mode to avoid data conversion. To use these tags, either 'B' or 'T' is used as the last character of the mode parameter.

Perform base64 encoding on the server side and base64 decoding on the client side


  

Use PHP to output input streams for operations

Function strToTwo ($ str) {$ res = ''; $ len = strlen ($ str); for ($ I = 0; $ I <$ len; $ I ++) {$ tmp = base_convert (ord ($ str [$ I]), 10, 2); $ res. = $ tmp. '';} return trim ($ res);} var_dump (strToTwo (" Yan "));

UTF-8 encoded. This time is 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.