时间:2021-07-01 10:21:17 帮助过:19人阅读
这是他的伪代码
仔细研究后,我翻译成PHP版的:
($n){
$bytes = [];
while (true){
array_unshift($bytes, bcmod($n, 128));
if($n < 128){
break;
}else{
$n = intval($n/128);
}
}
$bytes[count($bytes) - 1] += 128;
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] < 128){
$n = 128 * $n + $bytestream[$i];
}else{
$n = 128 * $n + ($bytestream[$i] - 128);
array_push($numbers, $n);
$n = 0;
}
}
return $numbers;
}
$a = encode([5, 130, 288]);
var_dump($a);
var_dump(decode($a));
打印出来的内容是:
array(5) { [0]=> int(133) [1]=> string(1)"1" [2]=> int(130) [3]=> string(1)"2" [4]=> int(160) }
array(3) { [0]=> int(5) [1]=> int(130) [2]=> int(288) }
//写二进制
$h = fopen('ejz3.txt', 'wb');
foreach ($a as $k => $v)
{
$str3 = pack('H*', sprintf("%02x", $v));
fwrite($h, $str3);
}
fclose($h);
//读二进制
$str2 = file_get_contents('ejz3.txt');
$str2 = unpack("H*", $str2);
$value = str_split($str2[1], 2);
foreach ($value as $k => $v)
{
$value[$k] = base_convert($v, 16, 10);
}
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了可变长字节码算法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。