-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
119 lines (106 loc) · 2.88 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
if (! function_exists('hex2string')) {
/**
* Convert readable hex-string to character string i.e. "41424344" => "ABCD".
*
* Source: https://github.com/micromys/Omnik/blob/master/inverter_class.php
* @param $hex
* @return string
*/
function hex2str($hex)
{
$string = '';
// process each pair of bytes
for ($i = 0; $i < strlen($hex) - 1; $i += 2) {
// pick 2 bytes, convert via hexdec to chr
$string .= chr(hexdec($hex[$i].$hex[$i + 1]));
}
return $string;
}
}
if (! function_exists('str2dec')) {
/**
* convert string to decimal i.e. string = 0x'0101' (=chr(1).chr(1)) => dec = 257.
*
* Source: https://github.com/micromys/Omnik/blob/master/inverter_class.php
* @param $string
* @return int
*/
function str2dec($string)
{
// reverse string 0x'121314'=> 0x'141312'
$str = strrev($string);
$dec = 0;
// foreach byte calculate decimal value multiplied by power of 256^$i
for ($i = 0; $i < strlen($string); $i++) {
// take a byte, get ascii value, multiply by 256^(0,1,...n where n=length-1) and add to $dec
$dec += ord(substr($str, $i, 1)) * pow(256, $i);
}
return $dec;
}
}
if (! function_exists('getLong')) {
/**
* Convert 4 bytes to decimal.
*
* @param string $string
* @param int $start
* @param int $divider
* @return float
*/
function getLong(string $string, int $start = 71, int $divider = 10)
{
$long = floatval(
str2dec(
substr(
$string,
$start,
4
)
)
);
return $long / $divider;
}
}
if (! function_exists('getShort')) {
/**
* Convert to decimal 2 bytes.
*
* @param string $string
* @param int $start
* @param int $divider
* @return float
*/
function getShort(string $string, int $start, int $divider)
{
$short = floatval(
str2dec(
substr(
$string,
$start,
2
)
)
);
// if 0xFFFF return 0 else value/divider
return ($short == 65535) ? 0 : $short / $divider;
}
}
if (! function_exists('getShortIterated')) {
/**
* @param string $string
* @param int $start
* @param int $divider
* @param int $offset
* @return array
*/
function getShortIterated(string $string, int $start, int $divider, int $offset = 2)
{
$values = [];
for ($i = 1; $i <= 3; $i++) {
$short = getShort($string, $start + $offset * ($i - 1), $divider);
$values[] = ($short == 65535) ? 0 : $short / $divider;
}
return $values;
}
}