Мое решение в PHP основано на ответе Python :
<?php
/**
* Decode 7-bit packed PDU messages
*/
if ( !function_exists( 'hex2bin' ) ) {
// pre 5.4 fallback
function hex2bin( $str ) {
$sbin = "";
$len = strlen( $str );
for ( $i = 0; $i < $len; $i += 2 ) {
$sbin .= pack( "H*", substr( $str, $i, 2 ) );
}
return $sbin;
}
}
function pdu2str($pdu) {
// chop and store bytes
$number = 0;
$bitcount = 0;
$output = '';
while (strlen($pdu)>1) {
$byte = ord(hex2bin(substr($pdu,0,2)));
$pdu=substr($pdu, 2);
$number += ($byte << $bitcount);
$bitcount++ ;
$output .= chr($number & 0x7F);
$number >>= 7;
if (7 == $bitcount) {
// save extra char
$output .= chr($number);
$bitcount = $number = 0;
}
}
return $output;
}
?>