| @@ 129-149 (lines=21) @@ | ||
| 126 | * @param boolean $l Optional. Little endian. Default value - false |
|
| 127 | * @return string Resulting binary string |
|
| 128 | */ |
|
| 129 | public static function int2bytes($len, $int = 0, $l = false) |
|
| 130 | { |
|
| 131 | $hexstr = dechex($int); |
|
| 132 | ||
| 133 | if ($len === null) { |
|
| 134 | if (mb_orig_strlen($hexstr) % 2) { |
|
| 135 | $hexstr = "0" . $hexstr; |
|
| 136 | } |
|
| 137 | } else { |
|
| 138 | $hexstr = str_repeat('0', $len * 2 - mb_orig_strlen($hexstr)) . $hexstr; |
|
| 139 | } |
|
| 140 | ||
| 141 | $bytes = mb_orig_strlen($hexstr) / 2; |
|
| 142 | $bin = ''; |
|
| 143 | ||
| 144 | for ($i = 0; $i < $bytes; ++$i) { |
|
| 145 | $bin .= chr(hexdec(substr($hexstr, $i * 2, 2))); |
|
| 146 | } |
|
| 147 | ||
| 148 | return $l ? strrev($bin) : $bin; |
|
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * Build byte |
|
| @@ 200-220 (lines=21) @@ | ||
| 197 | * @param boolean $l Optional. Little endian. Default value - true. |
|
| 198 | * @return string Resulting binary string |
|
| 199 | */ |
|
| 200 | public function int2bytes($len, $int = 0, $l = true) |
|
| 201 | { |
|
| 202 | $hexstr = dechex($int); |
|
| 203 | ||
| 204 | if ($len === null) { |
|
| 205 | if (mb_orig_strlen($hexstr) % 2) { |
|
| 206 | $hexstr = "0" . $hexstr; |
|
| 207 | } |
|
| 208 | } else { |
|
| 209 | $hexstr = str_repeat('0', $len * 2 - mb_orig_strlen($hexstr)) . $hexstr; |
|
| 210 | } |
|
| 211 | ||
| 212 | $bytes = mb_orig_strlen($hexstr) / 2; |
|
| 213 | $bin = ''; |
|
| 214 | ||
| 215 | for ($i = 0; $i < $bytes; ++$i) { |
|
| 216 | $bin .= chr(hexdec(substr($hexstr, $i * 2, 2))); |
|
| 217 | } |
|
| 218 | ||
| 219 | return $l ? strrev($bin) : $bin; |
|
| 220 | } |
|
| 221 | ||
| 222 | /** |
|
| 223 | * Send a packet |
|