Recently in the book "Large-scale Web services development technology". The book mentions the "Variable Long bytecode algorithm" algorithm for compressing data to achieve compressed data and reduce disk IO.
Variable long byte code algorithm:
The highest bit (subscript 7) of any byte is only used as the flag bit, and is multiplied by the corresponding power of 128 according to the position of the byte;
This is his pseudo-code.
After careful study, I translated into PHP version of:
($n) {$bytes= []; While(True){Array_unshift($bytes, Bcmod($n, +)); If($n < ){break;} else{$n= Intval($n/+); } }$bytes[Count($bytes)-1] += -; Return$bytes; } function encode($numbers){$bytestream= []; Foreach($numbers as $n){$bytestream= Array_merge($bytestream, Codenumber($n)); } return$bytestream; } function decode($bytestream){$numbers= [];$n=0; For($i = 0; $i < Count($bytestream); $i+ +){if($bytestream[$i] < ){$n= -*$n+$bytestream[$i]; }else{$n= -*$n+($bytestream[$i]- +); Array_push($numbers, $n);$n=0; }} return$numbers; }$a= Encode([5, 288]); Var_dump($a); Var_dump(decode($a)); The printed content is: array(5){ [0]=> int(133)[1]=> string(1)"1"[2]=> int()[3]=> string(1)"2"[4]=> int()}array(3){ [0]=> int(5)[1]=> int()[2]=> int(288)}//write binary$h= fopen(' ejz3.txt', ' WB '); Foreach($a as $k = $v){$STR 3= Pack(' h* ', sprintf("%02x", $v)); Fwrite($h, $str 3); } fclose($h); Read binary$str 2= file_get_contents(' ejz3.txt ');$str 2= Unpack("h*", $str 2);$value= Str_split($str 2[1], 2); Foreach($value as $k = $v){$value[$k] = Base_convert($v, +, ten); }
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above describes the variable long-byte code algorithm, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.