4
* Base 128 varints - decodes and encodes base128 varints to/from decimal
6
* @author Nikolai Kordulla
15
* @param int $modus - 1=Byte 2=String
17
public function __construct($modus)
19
$this->modus = $modus;
24
* @param $number - number as decimal
25
* Returns the base128 value of an dec value
27
public function set_value($number)
29
$string = decbin($number);
30
if (strlen($string) < 8) {
31
$hexstring = dechex(bindec($string));
32
if (strlen($hexstring) % 2 == 1) {
33
$hexstring = '0' . $hexstring;
35
if ($this->modus == 1) {
36
return $this->hex_to_str($hexstring);
41
// split it and insert the mb byte
42
$string_array = array();
44
while (strlen($string) > 0) {
45
if (strlen($string) < 8) {
46
$string = substr('00000000', 0, 7 - strlen($string) % 7) . $string;
49
$string_array[] = $pre . substr($string, strlen($string) - 7, 7);
50
$string = substr($string, 0, strlen($string) - 7);
52
if ($string == '0000000') {
58
foreach ($string_array as $string) {
59
$hexstring .= sprintf('%02X', bindec($string));
62
// now format to hexstring in the right format
63
if ($this->modus == 1) {
64
return $this->hex_to_str($hexstring);
72
* Returns the dec value of an base128
74
* @param string bstring
76
public function get_value($string)
78
// now just drop the msb and reorder it + parse it in own string
80
$string_length = strlen($string);
84
while ($string_length > $i) {
85
// unset msb string and reorder it
86
$valuestring = substr($string, $i, 7) . $valuestring;
91
return bindec($valuestring);
96
* Converts hex 2 ascii
98
* @param String $hex - the hex string
100
public function hex_to_str($hex)
104
for ($i = 0; $i < strlen($hex); $i += 2) {
105
$str .= chr(hexdec(substr($hex, $i, 2)));
4
* Base 128 varints - decodes and encodes base128 varints to/from decimal
6
* @author Nikolai Kordulla
15
* @param int $modus - 1=Byte 2=String
17
public function __construct($modus)
19
$this->modus = $modus;
24
* @param $number - number as decimal
25
* Returns the base128 value of an dec value
27
public function set_value($number)
29
$string = decbin($number);
30
if (strlen($string) < 8) {
31
$hexstring = dechex(bindec($string));
32
if (strlen($hexstring) % 2 == 1) {
33
$hexstring = '0' . $hexstring;
35
if ($this->modus == 1) {
36
return $this->hex_to_str($hexstring);
41
// split it and insert the mb byte
42
$string_array = array();
44
while (strlen($string) > 0) {
45
if (strlen($string) < 8) {
46
$string = substr('00000000', 0, 7 - strlen($string) % 7) . $string;
49
$string_array[] = $pre . substr($string, strlen($string) - 7, 7);
50
$string = substr($string, 0, strlen($string) - 7);
52
if ($string == '0000000') {
58
foreach ($string_array as $string) {
59
$hexstring .= sprintf('%02X', bindec($string));
62
// now format to hexstring in the right format
63
if ($this->modus == 1) {
64
return $this->hex_to_str($hexstring);
72
* Returns the dec value of an base128
74
* @param string bstring
76
public function get_value($string)
78
// now just drop the msb and reorder it + parse it in own string
80
$string_length = strlen($string);
84
while ($string_length > $i) {
85
// unset msb string and reorder it
86
$valuestring = substr($string, $i, 7) . $valuestring;
91
return bindec($valuestring);
96
* Converts hex 2 ascii
98
* @param String $hex - the hex string
100
public function hex_to_str($hex)
104
for ($i = 0; $i < strlen($hex); $i += 2) {
105
$str .= chr(hexdec(substr($hex, $i, 2)));