|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of mracine/php-binary-string. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Matthieu Racine <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace mracine\Helpers; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* class BinaryStringHelper |
|
14
|
|
|
* |
|
15
|
|
|
* Strings and binary datas manipulations |
|
16
|
|
|
* |
|
17
|
|
|
* @since 0.1.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
class BinaryStringHelper |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Convert an integer value in a "Network Byte Ordered" left trimed binary string (most significant value first) |
|
24
|
|
|
* |
|
25
|
|
|
* Reads the integer, starting from the most signficant byte, one byte a time. |
|
26
|
|
|
* Once reach a non 0 byte, construct a binary string representing this values |
|
27
|
|
|
* ex : |
|
28
|
|
|
* 0xFF7 => chr(0x0F).chr(0xF7) |
|
29
|
|
|
* 0x12345678 => chr(0x12).chr(0x34).chr(0x56).chr(0x76) |
|
30
|
|
|
* Compatible with 8, 16, 32, 64 etc.. bits systems |
|
31
|
|
|
* |
|
32
|
|
|
* @since 0.1.0 |
|
33
|
|
|
* @see https://en.wikipedia.org/wiki/Endianness |
|
34
|
|
|
* @param int $value the integer value to be converted to a Network Nyte Ordered string |
|
35
|
|
|
* @return string the binary string |
|
36
|
|
|
*/ |
|
37
|
5 |
|
public static function IntegerToNBOBinaryString(int $value) |
|
38
|
|
|
{ |
|
39
|
|
|
// Initialize an empty string |
|
40
|
5 |
|
$buffer = ''; |
|
41
|
|
|
// Lets start from the most significant byte |
|
42
|
5 |
|
for ($i=(PHP_INT_SIZE-1); $i>=0; $i--) { |
|
43
|
|
|
// Prepare a mask to keep only the most significant byte of $value |
|
44
|
5 |
|
$mask = 0xFF << ($i*8); |
|
45
|
|
|
// If the most significant byte is not 0, the final string must contain it |
|
46
|
|
|
// If we have already started to construct the string (i.e. there are more signficant digits) |
|
47
|
|
|
// we must set the byte, even if it is a 0. |
|
48
|
|
|
// 0xFF00FF, for example, require to set the second byte with a 0 value |
|
49
|
5 |
|
if (($value & $mask) || ''!==$buffer) { |
|
50
|
|
|
// Get the curent byte by shifting it to least significant position and add it to the string |
|
51
|
|
|
// 0xFF12345678 => 0xFF |
|
52
|
4 |
|
$byte = $value>>(8*$i); |
|
53
|
4 |
|
$buffer .= chr($byte); |
|
54
|
|
|
// Set the most significant byte to 0 so we can restart the process being shure |
|
55
|
|
|
// that the value is left padded with 0 |
|
56
|
|
|
// 0xFF12345678 => 0x12345678 |
|
57
|
|
|
// -1 = 0xFFFFF.... (number of F depend of PHP_INT_SIZE ) |
|
58
|
4 |
|
$mask = -1 >> ((PHP_INT_SIZE-$i)*8); |
|
59
|
4 |
|
$value &= $mask; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
// Special case, 0 will not fill the buffer, have to construct it manualy |
|
63
|
5 |
|
if (0==$value) { |
|
64
|
1 |
|
$buffer = chr(0); |
|
65
|
|
|
} |
|
66
|
5 |
|
return $buffer; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|