Jump to content

pdu function convert , need help


Recommended Posts

Hi

i 'm googing a function to convert string to PDU for sending USSD in autoit but still no luck, there is only code in php.

some one help me to convert this script to autoit? thanks in advance:

function str2pdu($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 .= binhex(substr($symbol,0,4)).binhex(substr($symbol,4));
  }
 
  return $pdu;
}
 
function pdu2str($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 .= binhex(substr($symbol,0,4)).binhex(substr($symbol,4));
  }
 
  return pack("H*", $hex);
}
 
function binhex($string)
{
  return strtoupper(dechex(bindec($string)));
}
 
Link to comment
Share on other sites

 

I'm sure someone will help you.

Post your code you need help with.

I'll start you off.

Func str2pdu($command)

    Return $pdu
EndFunc

ya my code to send AT command to modem look like:

#cs
syntax exemple: Huawei AT command
AT+CUSD=1,"*101#",15  ; check balance
AT+CUSD=1,"*100*123456789123#",15
where "*101#" or "*100*123456789123#"   user will enter in input1 :must pdu encode
 
#ce
; send AT command to modem
_commsendstring("AT+ & GUICtrlRead(str2pdu($input1) & 15" & @CR))
 
"*101#" pdu encode like  "2a31303123"
 
thank you
Link to comment
Share on other sites

 

ya my code to send AT command to modem look like:

#cs
syntax exemple: Huawei AT command
AT+CUSD=1,"*101#",15  ; check balance
AT+CUSD=1,"*100*123456789123#",15
where "*101#" or "*100*123456789123#"   user will enter in input1 :must pdu encode
 
#ce
; send AT command to modem
_commsendstring("AT+ & GUICtrlRead(str2pdu($input1) & 15" & @CR))
 
"*101#" pdu encode like  "2a31303123"
 
thank you

 

Do you want someone to just write the code for you?

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Dear John, Firefox,

maybe my bad English is not enough to explain, my main question is function to encode string to pdu that I think I can’t do it myself as beginner, I love autoit and would like it can do any thing like other language.

For the moment I can write a very basic script by learning the examples in this forum, if something so difficult I been here to ask the experience person.

Link to comment
Share on other sites

Please use autoit tags to post your code.

I don't know what the pack function outputs, so I replaced it with the StringToBinary/BinaryToString function.

#include <String.au3>

;~ function str2pdu($command)
Func str2pdu($command)
;~   $bin = "";
    Local $bin = ""
    Local $command2 = StringSplit($command, "", 2)
;~   for($i = 0; $i < strlen($command); $i++)
    For $i = 0 To StringLen($command) - 1
;~       $bin .= strrev(sprintf("%07b", ord($command[$i])));
        $bin &= _StringReverse(StringFormat("%07b", Asc($command2[$i])))
    Next
;~   $bin .= str_repeat("0", 8 - strlen($bin) % 8);
    $bin &= _StringRepeat("0", 8 - Mod(StringLen($bin), 8))
;~   $pdu = "";
    Local $pdu = ""
;~   while(strlen($bin))
    While (StringLen($bin))
;~     $symbol = substr($bin, 0, 8);
        $symbol = StringMid($bin, 1, 9)
;~     $symbol = strrev($symbol);
        $symbol = _StringReverse($symbol)
;~     $bin = substr($bin, 8);
        $bin = StringMid($bin, 9);
;~     $pdu .= binhex(substr($symbol,0,4)).binhex(substr($symbol,4));
        $pdu &= binhex(StringMid($symbol, 1, 5)) & binhex(StringMid($symbol, 5))
;~   }
    WEnd

;~   return $pdu;
    Return $pdu
;~ }
EndFunc   ;==>str2pdu

;~ function pdu2str($pduanswer)
Func pdu2str($pduanswer)
;~     $pdu = pack("H*", $pduanswer);
    Local $pdu = StringTrimLeft(StringToBinary($pduanswer), 2)
    Local $bin = "", $pdu2 = StringSplit($pdu, "", 2)
;~   for($i = 0; $i < strlen($pdu); $i++)
    For $i = 0 To StringLen($pdu) - 1
;~     $bin .= strrev(sprintf("%08b", ord($pdu[$i])));
        $bin &= _StringReverse(StringFormat("%08b", Asc($pdu2[$i])))
    Next

;~   $hex = "";
    Local $hex = ""
;~   while(strlen($bin)>=7)
    While StringLen($bin) >= 7
;~     $symbol = substr($bin, 0, 7);
        $symbol = StringMid($bin, 1, 8)
;~     $bin = substr($bin, 7);
        $bin = StringMid($bin, 8)

;~     $symbol = "0".strrev($symbol);
        $symbol = "0" & _StringReverse($symbol)
;~     $hex .= binhex(substr($symbol,0,4)).binhex(substr($symbol,4));
        $hex &= binhex(StringMid($symbol, 1, 5)) & binhex(StringMid($symbol, 5))
;~   }
    WEnd

;~     return pack("H*", $hex);
    Return BinaryToString("0x" & $hex)
;~ }
EndFunc   ;==>pdu2str

;~ function binhex($string)
Func binhex($string)
;~   return strtoupper(dechex(bindec($string)));
    Return StringUpper(Hex(Dec($string)))
;~ }
EndFunc   ;==>binhex
Br, FireFox. Edited by FireFox
Link to comment
Share on other sites

Please use autoit tags to post your code.

I don't know what the pack function outputs, so I replaced it with the StringToBinary/BinaryToString function.

#include <String.au3>

;~ function str2pdu($command)
Func str2pdu($command)
;~   $bin = "";
    Local $bin = ""
    Local $command2 = StringSplit($command, "", 2)
;~   for($i = 0; $i < strlen($command); $i++)
    For $i = 0 To StringLen($command) - 1
;~       $bin .= strrev(sprintf("%07b", ord($command[$i])));
        $bin &= _StringReverse(StringFormat("%07b", Asc($command2[$i])))
    Next
;~   $bin .= str_repeat("0", 8 - strlen($bin) % 8);
    $bin &= _StringRepeat("0", 8 - Mod(StringLen($bin), 8))
;~   $pdu = "";
    Local $pdu = ""
;~   while(strlen($bin))
    While (StringLen($bin))
;~     $symbol = substr($bin, 0, 8);
        $symbol = StringMid($bin, 1, 9)
;~     $symbol = strrev($symbol);
        $symbol = _StringReverse($symbol)
;~     $bin = substr($bin, 8);
        $bin = StringMid($bin, 9);
;~     $pdu .= binhex(substr($symbol,0,4)).binhex(substr($symbol,4));
        $pdu &= binhex(StringMid($symbol, 1, 5)) & binhex(StringMid($symbol, 5))
;~   }
    WEnd

;~   return $pdu;
    Return $pdu
;~ }
EndFunc   ;==>str2pdu

;~ function pdu2str($pduanswer)
Func pdu2str($pduanswer)
;~     $pdu = pack("H*", $pduanswer);
    Local $pdu = StringTrimLeft(StringToBinary($pduanswer), 2)
    Local $bin = "", $pdu2 = StringSplit($pdu, "", 2)
;~   for($i = 0; $i < strlen($pdu); $i++)
    For $i = 0 To StringLen($pdu) - 1
;~     $bin .= strrev(sprintf("%08b", ord($pdu[$i])));
        $bin &= _StringReverse(StringFormat("%08b", Asc($pdu2[$i])))
    Next

;~   $hex = "";
    Local $hex = ""
;~   while(strlen($bin)>=7)
    While StringLen($bin) >= 7
;~     $symbol = substr($bin, 0, 7);
        $symbol = StringMid($bin, 1, 8)
;~     $bin = substr($bin, 7);
        $bin = StringMid($bin, 8)

;~     $symbol = "0".strrev($symbol);
        $symbol = "0" & _StringReverse($symbol)
;~     $hex .= binhex(substr($symbol,0,4)).binhex(substr($symbol,4));
        $hex &= binhex(StringMid($symbol, 1, 5)) & binhex(StringMid($symbol, 5))
;~   }
    WEnd

;~     return pack("H*", $hex);
    Return BinaryToString("0x" & $hex)
;~ }
EndFunc   ;==>pdu2str

;~ function binhex($string)
Func binhex($string)
;~   return strtoupper(dechex(bindec($string)));
    Return StringUpper(Hex(Dec($string)))
;~ }
EndFunc   ;==>binhex
Br, FireFox.

 

dear Firefox,

 

i tried your code but it return many zero output

output of this function is hexadecimal 7 bit or 8 bit for GSM.

thanks for your times

 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...