PHP Realizes UTF8 Binary and Plaintext String Conversion Function Example
- 2021-08-16 23:26:51
- OfStack
This paper describes the conversion function of UTF82 binary and plaintext string by PHP. Share it for your reference, as follows:
<?php
/*********** This procedure is written by Yunke. Take it when you are free php Software development
* Satisfy 1 Take a look at the curiosity of novices UTF-8 Adj. 2 What is the binary system like
****************************/
define("b", "<br>");
$a = "FE";
$a1 = "FF";
$s = 16;
$e = 2;
echo $s . " Binary " . $a . " Represents as " . $e . " The binary system is " . base_convert($a, $s, $e) . b;
echo $s . " Binary " . $a1 . " Represents as " . $e . " The binary system is " . base_convert($a1, $s, $e) . b;
$str = " Computer rr Who are we? ";
$strlen = strlen($str);
$n = 0;
echo $str.' ( 2 Binary system UTF-8 Represents): '.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 into 2 Bimodal
$str_bin="1110100010101110101000011110011110101110100101111110011010011100101110100110000101110011111001101000100010010001111001001011101110101100111001101001100010101111111010001011000010000001111011111011110010011111"; // Enter here 2 Binary, the program code is 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;//2 Binary UTF8 Original code plaintext
?>
Run results:
16 Binary FE Represents as 2 The binary system is 11111110
16 Binary FF Represents as 2 The binary system is 11111111
Computer rr Who are we? ( 2 Binary system UTF-8 Represents):
1110100010101110101000011110011110101110100101111110011010011100101110100111001001110010111001101000100010010001111001001011101110101100111001101001100010101111111010001011000010000001111011111011110010011111
Computer as Who are we?
PS: Here are some related online coding conversion tools for your reference:
UTF-8 Transcoding Tools:
http://tools.ofstack.com/transcoding/convutf
Online Transcoding Tool (utf-8/utf-32/Punycode/Base64):
http://tools.ofstack.com/transcoding/decode_encode_tool
Native/UTF-8 Online Transcoding Tool:
http://tools.ofstack.com/transcoding/native2utf8
Online arbitrary binary conversion tool:
http://tools.ofstack.com/transcoding/hexconvert
More readers interested in PHP can check the topic of this site: "PHP Encoding and Transcoding Operation Skills Summary", "php Object-Oriented Programming Introduction Tutorial", "PHP Mathematical Operation Skills Summary", "PHP Array (Array) Operation Skills Encyclopedia", "php String (string) Usage Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php Regular Expression Usage Summary" and "php Common Database Operation Skills Summary"
I hope this article is helpful to everyone's PHP programming.