@@ 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 |
@@ 203-223 (lines=21) @@ | ||
200 | * @param boolean $l Optional. Little endian. Default value - true. |
|
201 | * @return string Resulting binary string |
|
202 | */ |
|
203 | public function int2bytes($len, $int = 0, $l = true) |
|
204 | { |
|
205 | $hexstr = dechex($int); |
|
206 | ||
207 | if ($len === null) { |
|
208 | if (mb_orig_strlen($hexstr) % 2) { |
|
209 | $hexstr = "0" . $hexstr; |
|
210 | } |
|
211 | } else { |
|
212 | $hexstr = str_repeat('0', $len * 2 - mb_orig_strlen($hexstr)) . $hexstr; |
|
213 | } |
|
214 | ||
215 | $bytes = mb_orig_strlen($hexstr) / 2; |
|
216 | $bin = ''; |
|
217 | ||
218 | for ($i = 0; $i < $bytes; ++$i) { |
|
219 | $bin .= chr(hexdec(substr($hexstr, $i * 2, 2))); |
|
220 | } |
|
221 | ||
222 | return $l ? strrev($bin) : $bin; |
|
223 | } |
|
224 | ||
225 | /** |
|
226 | * Send a packet |