Jump to content
Sign in to follow this  
chipchip

pdu function convert , need help

Recommended Posts

chipchip

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)));
}
 

Share this post


Link to post
Share on other sites
FireFox

Hi,

What I would do if I were you:

-Check what the PHP function does

-Search the function keywords in the autoit helpfile

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
chipchip

 

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

Share this post


Link to post
Share on other sites
chipchip

Hi,

What I would do if I were you:

-Check what the PHP function does

-Search the function keywords in the autoit helpfile

Br, FireFox.

i'm just beginer and amateur guy...

Share this post


Link to post
Share on other sites
FireFox

i'm just beginer and amateur guy...

Does it mean we have to make the script for you? No, otherwise you will never manage to do something by yourself.

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
FireFox

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

What's the relation with your first post?

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
JohnOne

 

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.

Share this post


Link to post
Share on other sites
chipchip

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.

Share this post


Link to post
Share on other sites
FireFox

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

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
chipchip

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

 

Share this post


Link to post
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
Sign in to follow this  

×