Variable long byte code algorithm

Source: Internet
Author: User
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.

  • 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.