Base64 the PHP version of the Custom encoding table,
In an interface docking, to use the Base64 Custom encoding table to encode and decode, from the online search, the principle of comparison is more thorough, the code provided by the example but not decoded, the following is the implementation of a Base64 custom dictionary decoding example, relatively coarse, Test the Assembly should be no problem, need this piece can take to see, first will others blog will be the principle of the take over
BASE64 coding is the encoding method that we often use in our program development. It is a representation of binary data based on a 64 printable character. It is commonly used as a way to store and transfer binary data encoding methods! It is also a common encoding method for the MIME (Multipurpose Internet Mail Extension, mainly used as an e-mail standard) to represent binary data in a printable character! It just defines a way to transfer content with printable characters, and does not produce a new character set! Sometimes, after we learn the idea of conversion, we can actually combine our own actual needs, to construct some of their own interface definition coding methods. All right, let's take a look at it and switch ideas!
Base64 Implementing the conversion principle
It is a binary all data method that is represented by 64 printable characters. Since 2 of the 6 is equal to 64, you can use every 6 bits as one unit, corresponding to a printable character. We know that three bytes have 24 bits, so we can just correspond to 4 Base64 units, that is, 3 bytes need to be represented by 4 Base64 printable characters. printable characters in Base64 include the letter A-Z, a-Z, number 0-9, a total of 62 characters, and two printable symbols that are generally different across systems. However, what we often call Base64 another 2 characters is: "+/". These 64 characters, the corresponding table is as follows.
numbering |
character |
|
|
|
|
|
|
| number character number character number character
|
|
0 |
A |
16 |
Q |
32 |
G |
48 |
W |
1 |
B |
17 |
R |
33 |
H |
49 |
X |
2 |
C |
18 |
S |
34 |
I |
50 |
Y |
3 |
D |
19 |
T |
35 |
J |
51 |
Z |
4 |
E |
20 |
U |
36 |
K |
52 |
0 |
5 |
F |
21st |
V |
37 |
L |
53 |
1 |
6 |
G |
22 |
W |
38 |
M |
54 |
2 |
7 |
H |
23 |
X |
39 |
N |
55 |
3 |
8 |
I |
24 |
Y |
40 |
O |
56 |
4 |
9 |
J |
25 |
Z |
41 |
P |
57 |
5 |
10 |
K |
26 |
A |
42 |
Q |
58 |
6 |
11 |
L |
27 |
B |
43 |
R |
59 |
7 |
12 |
M |
28 |
C |
44 |
S |
60 |
8 |
13 |
N |
29 |
D |
45 |
T |
61 |
9 |
14 |
O |
30 |
E |
46 |
U |
62 |
+ |
15 |
P |
31 |
F |
47 |
V |
63 |
/ |
When converting, the data of three bytes is placed in a 24bit buffer successively, and the first byte is the high. If the data is less than 3byte, the remaining bits in the buffer are filled with 0. Then, each time you remove 6 bits, follow the characters in their value selection
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
as the encoded output. Continue until the full input data conversion is complete.
If there are two input data left, add 1 "=" after the encoding result, and if there is a last input data, add 2 "=" after the encoding result, and if there is no data left, do not add anything, so as to guarantee the correctness of the data restoration.
The encoded data is slightly longer than the original data for the original 4/3. No matter what characters are all encoded, they are not encoded like quoted-printable, and some printable characters are retained. Therefore, it is less readable than quoted-printable encoding!
Text |
M |
A |
N |
ASCII encoding |
77 |
97 |
110 |
Bits |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
Index |
19 |
22 |
5 |
46 |
BASE64 encoding |
T |
W |
F |
U |
The ASCII code for M is 77, the first six digits correspond to 19, the corresponding Base64 character is T, and so on. Other character encodings can be automatically converted! Let's see if it's just a 3-byte case!
Text (1 bytes) |
A |
|
|
Bits |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Bits (complement 0) |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
BASE64 encoding |
Q |
Q |
= |
= |
Text (2 bytes) |
B |
C |
|
Bits |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
1 |
|
|
X |
X |
X |
X |
X |
X |
Bits (complement 0) |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
1 |
0 |
0 |
X |
X |
X |
X |
X |
X |
BASE64 encoding |
Q |
K |
M |
= |
This speaks very thoroughly, the original address: http://www.cnblogs.com/chengmo/archive/2014/05/18/3735917.html
Class base64{
Public $base 64_config = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' W ', ' X ', ' y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' _ ', '-'];
Public Function GetBytes ($string) {
$data = Iconv ("UTF-8", "GBK", $string);
Return unpack ("c*", $data);
}
Public Function Array_index ($t) {
Return Array_search ($t, $this->base64_config);
}
Public function decode ($STR) {
$str = Str_replace ("!", "" ", $str);
$slen = strlen ($STR);
$mod = $slen% 4;
$num = Floor ($slen/4);
$desc = [];
for ($i =0; $i < $num; $i + +) {
$arr = Array_map ("Base64::array_index", Str_split (substr ($str, $i *4,4)));
$desc _0 = ($arr [0]<<2) | (($arr [1]&48) >>4);
$desc _1 = (($arr [1]&15) <<4) | (($arr [2]&60) >>2);
$desc _2 = (($arr [2]&3) <<6) | $arr [3];
$desc = Array_merge ($desc, [$desc _0, $desc _1, $desc _2]);
}
if ($mod = = 0) return implode (", Array_map (" Chr ", $desc));
$arr = Array_map ("Base64::array_index", Str_split (substr ($str, $num *4,4)));
if (count ($arr) = = 1) {
$desc _0 = $arr [0]<<2;
if ($desc _0! = 0) $desc = Array_merge ($desc, [$desc _0]);
}else if (count ($arr) = = 2) {
$desc _0 = ($arr [0]<<2) | (($arr [1]&48) >>4);
$desc = Array_merge ($desc, [$desc _0]);
}else if (count ($arr) = = 3) {
$desc _0 = ($arr [0]<<2) | (($arr [1]&48) >>4);
$desc _1 = ($arr [1]<<4) | (($arr [2]&60) >>2);
$desc = Array_merge ($desc, [$desc _0, $desc _1]);
}
Return implode (", Array_map (" Chr ", $desc));
}
Public function encode ($STR) {
$byte _arr = $this->getbytes ($STR);
$slen =count ($byte _arr);
$smod = ($slen%3);
$snum = Floor ($slen/3);
$desc = Array ();
for ($i =1; $i <= $snum; $i + +) {
$index _num = ($i-1);
$_dec0= $byte _arr[$index _num+1]>>2;
$_dec1= (($byte _arr[$index _num+1]&3) <<4) | ($byte _arr[$index _num+2]>>4);
$_dec2= (($byte _arr[$index _num+2]&0xf) <<2) | ($byte _arr[$index _num+3]>>6);
$_dec3= $byte _arr[$index _num+3]&63;
$desc = Array_merge ($desc, Array ($this->BASE64_CONFIG[$_DEC0], $this->BASE64_CONFIG[$_DEC1], $this->base64 _CONFIG[$_DEC2], $this->BASE64_CONFIG[$_DEC3]));
}
if ($smod ==0) return implode (", $DESC);
$n = ($snum) +1;
$_dec0= $byte _arr[$n]>>2;
Only one byte
if (!isset ($byte _arr[$n +1])) {
$_dec1= (($byte _arr[$n]&3) <<4);
$_dec2=$_dec3= "!";
}else{
2 bytes
$_dec1= (($byte _arr[$n]&3) <<4) | ($byte _arr[$n +1]>>4);
$_dec2= $this->base64_config[($byte _arr[$n +1]&0xf) <<2];
$_dec3= "!";
}
$desc = Array_merge ($desc, Array ($this->BASE64_CONFIG[$_DEC0], $this->base64_config[$_dec1],$_dec2,$_dec3));
Return implode (", $DESC);
}
}
$base = new Base64 ();
Echo Array_search ("E", $base 64->base64_config);
Exit
$tt = $base 64->encode ("Chinese is not afraid of asdasdas23232323. 、");
echo $tt. "
";
$TTT = $base 64->decode ($TT);
echo $ttt. "
";
http://www.bkjia.com/PHPjc/1084384.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084384.html techarticle Base64 Custom Encoding table PHP version, in an interface docking, to use the Base64 Custom encoding table to encode and decode, from the Internet to search for a bit, the principle of comparison and more ...