| Con questa UDF si puņ convertire un Testo in Base64 e viceversa, inoltre vi sono le funzioni _BinToDec() e _DecToBin() (che servono per Base64) che convertono una stringa binaria in decimale e viceversa: CODICE _Esempio() Func _Esempio() $GUI = GUICreate("Base64", 480, 108, 193, 125) GUICtrlCreateLabel("Testo", 8, 8, 52, 17) $text = GUICtrlCreateInput("TESTO", 64, 8, 409, 21) GUICtrlCreateLabel("Base64", 8, 40, 52, 17) $base64 = GUICtrlCreateInput("", 64, 40, 409, 21) $Testo64 = GUICtrlCreateButton("Testo -> Base64", 72, 72, 153, 33, 0) $64Testo = GUICtrlCreateButton("Base64 -> Testo", 264, 72, 153, 33, 0) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case - 3 Exit Case $Testo64 If GUICtrlRead($text) = "" Then ContinueLoop GUICtrlSetData($base64, _Base64_en(GUICtrlRead($text))) Case $64Testo If GUICtrlRead($base64) = "" Then ContinueLoop GUICtrlSetData($text, _Base64_de(GUICtrlRead($base64))) EndSwitch WEnd EndFunc ;==>_Esempio Func _Base64_de($sHash) Dim $B64_Code[64] = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"] Dim $sDE_HASH_bin = "", $sB64_Ret_R = "" For $i_i = 1 To StringLen($sHash) If StringMid($sHash, $i_i, 1) = "=" Then $sDE_HASH_bin &= "000000" Else For $i_ii = 0 To 63 If $B64_Code[$i_ii] == StringMid($sHash, $i_i, 1) Then $sDE_HASH_bin &= _DecToBin($i_ii, 6) ExitLoop EndIf Next EndIf Next $aB64_Split_R = _StringInBlock($sDE_HASH_bin, 8) For $i = 1 To $aB64_Split_R[0] $sB64_Ret_R &= Chr(_BinToDec($aB64_Split_R[$i])) Next Return $sB64_Ret_R EndFunc ;==>_Base64_de Func _Base64_en($sText) Dim $B64_Code[64] = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"] Dim $sBinText = "", $nNULL = 0, $sB64_Ret = "" For $i = 1 To StringLen($sText) $sBinText &= _DecToBin(Asc(StringMid($sText, $i, 1))) Next If Mod(StringLen($sBinText), 6) = 2 Then $sBinText &= "00" $nNULL = 1 EndIf If Mod(StringLen($sBinText), 6) = 4 Then $sBinText &= "0000" $nNULL = 2 EndIf $aB64_Split = _StringInBlock($sBinText, 6) For $i = 1 To $aB64_Split[0] $sB64_Ret &= $B64_Code[Number(_BinToDec($aB64_Split[$i]))] Next If $nNULL = 1 Then $sB64_Ret &= "=" If $nNULL = 2 Then $sB64_Ret &= "==" Return StringReplace($sB64_Ret,"A=","") EndFunc ;==>_Base64_en Func _StringInBlock($sString, $nBlock) Dim $aRet[Int(StringLen($sString) / $nBlock) + 2] = [Int(StringLen($sString) / $nBlock) + 1] If IsInt(Number(StringLen($sString)) / Number($nBlock)) Then Dim $aRet[Int(StringLen($sString) / $nBlock) + 1] = [Int(StringLen($sString) / $nBlock)] For $i_ = 1 To $aRet[0] If $i_ = $aRet[0] Then $aRet[$i_] = StringMid($sString, $nBlock * ($i_ - 1) + 1) Else $aRet[$i_] = StringMid($sString, $nBlock * ($i_ - 1) + 1, $nBlock) EndIf Next Return $aRet EndFunc ;==>_StringInBlock Func _DecToBin($nNum, $Dim = 8) If $nNum = 0 Then $eRet = "" For $i = 1 To $Dim $eRet &= "0" Next Return $eRet EndIf Local $sRet = "", $nCurr = $nNum, $sRet_Rev = "" While $nCurr <> 1 $sRet &= String(Mod($nCurr, 2)) $nCurr = Ceiling($nCurr / 2) - Mod($nCurr, 2) WEnd $sRet &= "1" For $i = StringLen($sRet) To $Dim - 1 $sRet &= "0" Next For $i = 0 To StringLen($sRet) - 1 $sRet_Rev &= StringMid($sRet, StringLen($sRet) - $i, 1) Next Return $sRet_Rev EndFunc ;==>_DecToBin Func _BinToDec($sBin) Local $nInt = 0, $sBinRev = "" For $i = 0 To StringLen($sBin) - 1 $sBinRev &= StringMid($sBin, StringLen($sBin) - $i, 1) Next For $i = 0 To StringLen($sBinRev) - 1 $nInt += Number(StringMid($sBinRev, 1 + $i, 1)) * (2 ^ ($i)) Next Return $nInt EndFunc ;==>_BinToDec Nel codice sopra c'č anche un esempio. C'č un errore perņ: alcune volte, aggiunge alla stringa convertita in Base64, un valore nullo "A="( che perņ non influenza la stringa nel caso si voglia decodificare), e non sono riuscito a capire perchč... Comunque, aspetto commenti EDIT: Errore sistemato Edited by MatteoJug - 14/7/2008, 22:01
|