@@ 181-201 (lines=21) @@ | ||
178 | * @param boolean $l Optional. Little endian. Default value - true. |
|
179 | * @return string Resulting binary string |
|
180 | */ |
|
181 | public function int2bytes($len, $int = 0, $l = true) |
|
182 | { |
|
183 | $hexstr = dechex($int); |
|
184 | ||
185 | if ($len === null) { |
|
186 | if (mb_orig_strlen($hexstr) % 2) { |
|
187 | $hexstr = "0" . $hexstr; |
|
188 | } |
|
189 | } else { |
|
190 | $hexstr = str_repeat('0', $len * 2 - mb_orig_strlen($hexstr)) . $hexstr; |
|
191 | } |
|
192 | ||
193 | $bytes = mb_orig_strlen($hexstr) / 2; |
|
194 | $bin = ''; |
|
195 | ||
196 | for ($i = 0; $i < $bytes; ++$i) { |
|
197 | $bin .= chr(hexdec(substr($hexstr, $i * 2, 2))); |
|
198 | } |
|
199 | ||
200 | return $l ? strrev($bin) : $bin; |
|
201 | } |
|
202 | ||
203 | /** |
|
204 | * Send a packet |
@@ 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 |