Convert php Chinese characters to binary decimal hexadecimal numerals

Source: Internet
Author: User
Php Chinese characters are converted into binary-decimal hexadecimal numbers & lt ;? Php/*** converts a Chinese character into a unicode universal function without any library, and other user-defined functions, but with the following conditions: this file and the input parameters of the function should be UTF-8 encoded, otherwise function conversion is required * In fact, the reverse conversion function can also be easily compiled. php Chinese characters can be converted to binary hexadecimal numbers.
 "; Echo" good ". getUnicodeFromOneUTF8 (" good "); echo"
"; Echo" Hello ". getUnicodeFromOneUTF8 (" Hello "); echo"
"; Echo" how are you ". getUnicodeFromOneUTF8 (""); 20320 4.4.21952182258e + 21 */function getUnicodeFromOneUTF8 ($ word) {// Obtain the internal array representation of its characters, therefore, this file uses UTF-8 encoding! If (is_array ($ word) $ arr = $ word; else $ arr = str_split ($ word); // at this time, $ arr should be similar to array (228,189,160) // define an empty string to store $ bin_str = ''; // Convert it to a number, convert it to a binary string, and then combine it. Foreach ($ arr as $ value) $ bin_str. = decbin (ord ($ value); // at this time, $ bin_str should be similar to 111001001011110110100000. if it is a Chinese character "you" // regular cut $ bin_str = preg_replace ('/^. {4 }(. {4 }). {2 }(. {6 }). {2 }(. {6}) $/',' $1 $2 $ 3', $ bin_str); // at this time, $ bin_str should be similar to 0100111101100000, if it is a Chinese character "you" return bindec ($ bin_str); // return something similar to 20320, Chinese character "you" // return dechex (bindec ($ bin_str )); // If you want to return the hexadecimal 4f60, use this sentence} echo "you ". getUnicodeFromOneUTF8 ("you"); echo"
"; Echo" good ". getUnicodeFromOneUTF8 (" good "); echo"
"; Echo" Hello ". getUnicodeFromOneUTF8 (" Hello "); echo"
"; Echo" ". getUnicodeFromOneUTF8 (" "); exit;?>
?

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.