PHP 1bit padding for PDU GSM Multipart

Hi, can someone please help to such?
I thought it had absolutely crazy :frowning:

A need encode string in 7-bit and make 1bit padding.

My function is:
[php]
public function encode7bit($text, $paddingBits = false){
$ret = ‘’;
$data = str_split($text);
$mask = 0xFF;
$shift = 0;
$len = count($data);
//$paddingBits = 1;

	/*if($paddingBits){
		 $shift = 7 - $paddingBits;
		 $ret .= substr($text, 0, 1) << (7 - $shift);
		 $shift++;
	 }*/
	
    for ($i = 0; $i < $len; $i++) {
            $char = ord($data[$i]) & 0x7F; // only 7bits
            $nextChar = ($i+1 < $len) ? (ord($data[$i+1]) & 0x7F) : 0; // only 7bits
            if ($shift == 7) { $shift = 0; continue; }
            $carry  = ($nextChar & ((($mask << ($shift+1)) ^ 0xFF) & 0xFF));
            $digit = (($carry << (7-$shift)) | ($char >> $shift) ) & 0xFF;
            $ret .= chr($digit);
            $shift++;
    }
		
    $str = unpack('H*', $ret);
    return $str[1];
}

[/php]

Thank you for any help

Sponsor our Newsletter | Privacy Policy | Terms of Service