Naombeni msaada kwenye tatizo la PHP

Naombeni msaada kwenye tatizo la PHP

MK254

JF-Expert Member
Joined
May 11, 2013
Posts
33,480
Reaction score
53,342
Poleni kwa majukumu coders wenzangu, hapa nina tatizo ambalo linanizingua na ninahitaji a second eye kutoka kwenu. Natumaini Kiswahili changu cha Kenya kitaeleweka.

Kuna mradi ninaofanya na nahitajika kutumia SMS kubeba payload. Tatizo kama tujuavyo SMS huwa limit yake ni 161 characters hivyo siwezi nikasafirisha bytes za kutosha.

Sasa imenilazimu kutumia mfumo wa CODE3TO4 ambao unaniwezesha kupunguza kila byte inabeba 6 bits badala ya kawaida 8 bits. Kwa ufupi, ujumbe mmoja wa SMS ukiwa encoded na huu mfumo unakuwezesha kubeba payload kubwa ambayo haingewezekana.

kwa mfano tu, ujumbe kama huu wa SMS - ######E*####################kGw#####FJJ#####5E
Ukiwa parsed unakupa HEX zifuatazo - 63 73 4c 30 30 30 31 30 35 35 32 30 39 39 0 32 43 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 b0 c4 3 0 0 0 51 55 1 0 0 XX Hapo ni pamoja na checksum.

Sasa tatizo langu lipo kwenye code, nimefaulu na kila kitu kimeenda sawa kwa muda, lakini ghafla na intermittently imekua iki-misbehave, unakuta payload niliyotegemea siyo yaani baada ya kuwa parsed, ilhali sijabadilisha chochote kwenye logics.

Hebu muitazame code yangu hii hapa.

Code:
    //load 3to4 encoding first
        $handle = @fopen("3to4.txt", "r");
        if ($handle) {
            while (($buffer = fgets($handle, 4096)) !== false) {
                $buffer = explode(',',$buffer);
                $_3to4[$buffer[0]] = trim($buffer[1]); 
                
            }
            if (!feof($handle)) {
                die("Error: unexpected fgets() fail");
            }


            fclose($handle);
        }
    


            
            $sms_text = "##AQypz3ujYRvyyal*#IQE############wzv1##0F";
            
            //Divide into groups of 4 bytes each
            $dat = str_split($sms_text,4);
            
                //loop through the bytes
                foreach($dat as $d){
                    
                    //convert into array and reverse
                    $b = array_reverse(str_split($d));
                    
                    $flipped_3to4 .= implode($b)."  ";
                        
                    //convert each element into hex using 3to4
                    foreach($b as $a){
                        
                        $hex_val = array_search($a,$_3to4);
                        if($hex_val >'' || $hex_val ==0){
                                $bin_str .= str_pad(base_convert($hex_val,'16','2'),6,"0",STR_PAD_LEFT); 
                        }else{
                            $missing .= $a;
                        }
                                
                    }
                    
                        $hex_str = str_pad(base_convert($bin_str,'2','16'),6,"0",STR_PAD_LEFT);
                        $raw .= $hex_str;
                        
                        //then flip back
                        $hex_ar = str_split($hex_str,2);
                        foreach($hex_ar as $h){    
                            //reverse for least significant byte
                            $str .= strrev($h)." ";
                        }    
                        $hex_str .= strrev($str);
                        
                        //clear vars
                        $hex_str = '';
                        $bin_str = '';
                        $str = '';


                        
                }


                echo $hex_str;
 
Kwanza kibisa cheki hiyo function yako inatakiwa iwe fopen badala yake umeandika @fopen!!!!
 
Kwanza kibisa cheki hiyo function yako inatakiwa iwe fopen badala yake umeandika @fopen!!!!

Asante kaka, ila hiyo hapo nimetumia @ operator inayosaidia ku-suppress errors, angalia taarifa zaidi hapa PHP: Error Control Operators - Manual

Ninachosema ni kwamba, code inafanya kazi, ila kuna wakati haileti results ninazotarajia.
 
Jaribu kukagua mahali panapo sumbua kwa kuweka print syntax. e.g

//loop through the bytes
foreach($dat as $d){

//convert into array and reverse
$b = array_reverse(str_split($d));
print $b;

Hii inaweza kukusaidia kujua kwanini inaleta results usizozitarajia
 
Kuna mradi ninaofanya na nahitajika kutumia SMS kubeba payload. Tatizo kama tujuavyo SMS huwa limit yake ni 161 characters hivyo siwezi nikasafirisha bytes za kutosha.

Sasa imenilazimu kutumia mfumo wa CODE3TO4 ambao unaniwezesha kupunguza kila byte inabeba 6 bits badala ya kawaida 8 bits. Kwa ufupi, ujumbe mmoja wa SMS ukiwa encoded na huu mfumo unakuwezesha kubeba payload kubwa ambayo haingewezekana.

Sasa tatizo langu lipo kwenye code, nimefaulu na kila kitu kimeenda sawa kwa muda, lakini ghafla na intermittently imekua iki-misbehave, unakuta payload niliyotegemea siyo yaani baada ya kuwa parsed, ilhali sijabadilisha chochote kwenye logics.

Huu mfumo wa CODE3TO4 sipo familiar nao, je is it a well known compression algorithm that has been well tested?

I hope I wont be going off topic if I suggest a solution that accomplishes something close to what you want:

Kuna inbuilt PHP library unaweza ukatumia kufanya compression kwa long strings (for short strings i just dont see its use it just adds additional headers and the string might actually be longer)

PHP: gzdeflate - Manual

Here is a simple usage of the library
Code:
 [/FONT]
<?php 
$string = "I am a long string very long string just try to compress me and you will see how compressed I can get 
           I am a long string very long string just try to compress me and you will see how compressed I can get
           I am a long string very long string just try to compress me and you will see how compressed I can get
           I am a long string very long string just try to compress me and you will see how compressed I can get
           ";


echo "Length of Uncompressed String:".strlen($string);




$compressed = gzdeflate($string,  9);




echo '<br/>Length of Compressed String:'.strlen($compressed).'<br/>';
echo $compressed;


echo gzinflate(gzinflate($compressed));
?>
[FONT=Verdana]

I might try to play around with your code nione kama nitaweza to debug it.
 
Jaribu kukagua mahali panapo sumbua kwa kuweka print syntax. e.g

//loop through the bytes
foreach($dat as $d){

//convert into array and reverse
$b = array_reverse(str_split($d));
print $b;

Hii inaweza kukusaidia kujua kwanini inaleta results usizozitarajia

Asante, huwa natumia debugger ya PHP Debugger ambayo inarahisha step through badala ya kutumia print ama echo kila mahali, ila nimefaulu ku-solve, tatizo lilikua kwenye payload, kuna extra character iliyokua inasababisha hayo. Jameni asante kwa kutumia muda wako.
 
Huu mfumo wa CODE3TO4 sipo familiar nao, je is it a well known compression algorithm that has been well tested?

I hope I wont be going off topic if I suggest a solution that accomplishes something close to what you want:

Kuna inbuilt PHP library unaweza ukatumia kufanya compression kwa long strings (for short strings i just dont see its use it just adds additional headers and the string might actually be longer)

PHP: gzdeflate - Manual

Here is a simple usage of the library
Code:
<?php 
$string = "I am a long string very long string just try to compress me and you will see how compressed I can get 
           I am a long string very long string just try to compress me and you will see how compressed I can get
           I am a long string very long string just try to compress me and you will see how compressed I can get
           I am a long string very long string just try to compress me and you will see how compressed I can get
           ";


echo "Length of Uncompressed String:".strlen($string);




$compressed = gzdeflate($string,  9);




echo '<br/>Length of Compressed String:'.strlen($compressed).'<br/>';
echo $compressed;


echo gzinflate(gzinflate($compressed));
?>
I might try to play around with your code nione kama nitaweza to debug it.

Thanks, though I managed to solve the issue but I would like to explain a little more so this can be clear even for anyone following through silently.

One, PHP compression can't be used here as am talking about disparate systems, meaning the text is compressed by a device by encoding it using CODE3TO4 algorithm then sent to a TCPIP port where the receiving application (can be any language C++ etc) has to decode it to hexadecimal and then finally to ascii in order to derive human readable text.
 

Similar Discussions

Back
Top Bottom