-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathConverter.php
54 lines (45 loc) · 1.13 KB
/
Converter.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
<?php
namespace Pdu;
/**
* A simple Pdu Converter utility. Funny enough, this is hard to come by
* All credit to https://github.com/gnomeby/ussd
*/
class Converter
{
public static function toPDU($command)
{
$bin = "";
for($i = 0; $i < strlen($command); $i++)
$bin .= strrev(sprintf("%07b", ord($command[$i])));
$bin .= str_repeat("0", 8 - strlen($bin) % 8);
$pdu = "";
while(strlen($bin))
{
$symbol = substr($bin, 0, 8);
$symbol = strrev($symbol);
$bin = substr($bin, 8);
$pdu .= static::binhex(substr($symbol,0,4)).static::binhex(substr($symbol,4));
}
return $pdu;
}
public static function toText($pduanswer)
{
$pdu = pack("H*", $pduanswer);
$bin = "";
for($i = 0; $i < strlen($pdu); $i++)
$bin .= strrev(sprintf("%08b", ord($pdu[$i])));
$hex = "";
while(strlen($bin)>=7)
{
$symbol = substr($bin, 0, 7);
$bin = substr($bin, 7);
$symbol = "0".strrev($symbol);
$hex .= static::binhex(substr($symbol,0,4)).static::binhex(substr($symbol,4));
}
return pack("H*", $hex);
}
protected static function binhex($string)
{
return strtoupper(dechex(bindec($string)));
}
}