Example of converting UTF8 binary and plaintext strings using PHP,

Source: Internet
Author: User
Tags php software

Example of converting UTF8 binary and plaintext strings using PHP,

This example describes how PHP converts UTF8 binary and plaintext strings. We will share this with you for your reference. The details are as follows:

<? Php/************ this program is compiled by yunke. Take on php software development when you are free. * satisfy the curiosity of new users, look at what the binary of the UTF-8 looks like dropping ***************************/define (" B ", "<br>"); $ a = "FE"; $ a1 = "FF"; $ s = 16; $ e = 2; echo $ s. "hexadecimal ". $. "indicates ". $ e. "in hexadecimal format ". base_convert ($ a, $ s, $ e ). b; echo $ s. "hexadecimal ". $ a1. "indicates ". $ e. "in hexadecimal format ". base_convert ($ a1, $ s, $ e ). b; $ str = "Who are we with computer rr? "; $ Strlen = strlen ($ str); $ n = 0; echo $ str. '(Binary UTF-8 representation ):'. b; $ str_bin = ''; while ($ n <$ strlen) {$ t = ord ($ str [$ n]); $ stra = base_convert ($ t, 10, 2); if (strlen ($ stra) <8) {$ stra = "0 ". $ stra;} $ str_bin. = $ stra; $ n ++;} echo $ str_bin. B; // has been translated as binary $ str_bin = "1110100010101110101000011110011110101110100101111110011010011100101110100110000101110011111001101000100010010001111001001011101110101100111001101001100010 101111111010001011000010000001111011111011110010011111 "; // enter the binary here. The program is encoded as the plaintext output $ chr =''; $ str = ''; for ($ I = 0; $ I <strlen ($ str_bin); $ I ++) {$ chr. = $ str_bin [$ I]; if ($ I + 1) % 8 = 0) {$ str. = chr (base_convert ($ chr, 2, 10); $ chr = NULL;} echo $ str; // The plaintext of the binary UTF8 source code?>

Running result:

In hexadecimal mode, FE is represented as binary mode. In binary mode, FF is represented as binary mode. In binary mode, rr is 1111111016. Who are we? (Binary UTF-8): 1110100010101110101000011110011110101110100101111110011010011100101110100111001001110010111001101000100010010001111001001011101110101100111001101001100010101111111010001011000010000001111011111011110010011111 computers as Who Are We?

PS: I would like to recommend several online encoding and conversion tools for your reference:

UTF-8 encoding conversion tool:
Http://tools.jb51.net/transcoding/convutf

Online encoding conversion tool (UTF-8/utf-32/Punycode/Base64 ):
Http://tools.jb51.net/transcoding/decode_encode_tool

Native/UTF-8 online encoding conversion tool:
Http://tools.jb51.net/transcoding/native2utf8

Any online Conversion Tool:
Http://tools.jb51.net/transcoding/hexconvert

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.