最新消息:

URLEncode_udf[转换字符为ANSI|Unicode UTF 8(16)URL编码及逆转还原]

autoit 永恒心锁 1644浏览 0评论

修改_StringToEncode函数,添加一个参数,以输出形如“2D 4E 87 65 41 00 42 00 43 00 31 00 32 00 33 00”
顺便修改了函数命名

;例子:
$str = "?a测试test_搜索 abc +1234?百度. "
$str1 = _StringToEncode($str)
MsgBox(0, '_StringToEncode _EncodeToString 转换前后', $str & @CRLF & $str1 & @CRLF & _EncodeToString($str1))
;ShellExecute('http://www.baidu.com/s?wd=' & $str1)

;例子:
$str2 = 'http://www.autoitx.com'
$str21 = _StringToEncode($str2, 1, 1)
MsgBox(0, '_StringToEncode _EncodeToString 转换前后', $str2 & @CRLF & $str21 & @CRLF & _EncodeToString($str21))

$str = '中文ABC123'
MsgBox(0, '_StringToEncode 转换前后', $str & @CRLF & _StringToEncode($str, 2, 1, ' '))


;例子:
$str = "测试test_.,;[]AB C012.0好"
$str1 = _StringToUnicodeEncode($str)
$str4 = _UnicodeEncodeToString($str1)
MsgBox(0, '_StringToUnicodeEncode _UnicodeEncodeToString 转换前后', $str & @CRLF & $str1 & @CRLF & $str4)



Func _StringToEncode($str, $Encode = 1, $AllFlag = 0, $sPrefix = '%')
 ;afan 提示:转换字符串为ANSI;GB2312;URL;unicode等格式编码
 ;$str - 需转换的字符串
 ;$Encode - [可选参数]编码格式: 1(默认) = ANSI; 2 = UTF16 小; 3 = UTF16 大; 4 = UTF8
 ;$AllFlag - [可选参数] =0 不转换字母及数字(默认); =1 全部转换(字符头http://除外)
 ;$sPrefix - [可选参数]编码首字符
 ;返回值:成功 - 返回转换后的编码字符串; 失败 - 设置 @Error = 1

 Local $str_s = StringRegExpReplace($str, '^\s*(?i)(http://).+', '$1')
 If Not @extended Then $str_s = ''
 If @extended Then $str = StringRegExpReplace($str, '^\s*(?i)http://', '')
 Local $sOut, $i, $sS2B, $aSR
 If $AllFlag Then
 $sS2B = StringTrimLeft(StringToBinary($str, $Encode), 2)
 $sOut = StringRegExpReplace($sS2B, '..', $sPrefix & '$0')
 Else
 $sOut = $str
 $aSR = StringRegExp($str, '\W+', 3)
 If Not @Error Then
 For $i = 0 To UBound($aSR) - 1
 $sS2B = StringTrimLeft(StringToBinary($aSR[$i], $Encode), 2)
 $sOut = StringReplace($sOut, $aSR[$i], StringRegExpReplace($sS2B, '..', $sPrefix & '$0'), 1)
 Next
 EndIf
 EndIf
 If $sPrefix = ' ' Then $sOut = StringRegExpReplace($sOut, '^\h+|\h+


, '')
 ;$sOut = StringReplace($sOut, '%20', '+') ;转换空格的编码为'+'号,也可不要此行
 Return $str_s & $sOut
EndFunc ;==>_StringToEncode

Func _EncodeToString($sEncode, $Encode = 1)
 ;afan 提示:转换经ANSI;GB2312;URL;unicode等格式编码后的字符串为原始字符串
 ;$sEncode - 编码字符串
 ;$Encode - [可选参数]URL编码格式 ;1 (默认) = ANSI ;2 = UTF16 小 ;3 = UTF16 大 ;4 = UTF8
 ;返回值:成功 - 返回编码字符串的原始字符串; 失败 - 返回原字符串并设置 @Error = 1

 Local $Rstr = $sEncode, $aSR, $str_Tmp, $i
 ;$Rstr = StringReplace($sEncode, '+', ' ') ;转换+为空格,也可不要此行
 $aSR = StringRegExp($Rstr, '(?:%\w{2})+', 3)
 If @error Then Return SetError(1, 0, $Rstr)
 For $i = 0 To UBound($aSR) - 1
 $str_Tmp = BinaryToString('0x' & StringReplace($aSR[$i], '%', ''), $Encode)
 $Rstr = StringReplace($Rstr, $aSR[$i], $str_Tmp, 1)
 Next
 Return $Rstr
EndFunc ;==>_EncodeToString

Func _StringToUnicodeEncode($str, $sPrefix = '&#x', $sSuffix = ';')
 ;afan 提示:转换字符串为Unicode UTF 编码
 ;$str - 需转换的字符串
 ;$sPrefix - [可选参数]前缀字符
 ;$sSuffix - [可选参数]后缀字符
 ;返回值:成功 - 返回转换后的编码字符串

 Local $EncodedString, $i, $s2d, $sS = StringSplit($str, '')
 For $i = 1 To $sS[0]
 If StringRegExp($sS[$i], '[^\x00-\xff]') Then
 $EncodedString &= $sPrefix & Hex(AscW($sS[$i]), 4) & $sSuffix
 Else
 $EncodedString &= $sS[$i]
 EndIf
 Next
 Return $EncodedString
EndFunc ;==>_StringToUnicodeEncode

Func _UnicodeEncodeToString($sEncode, $sPrefix = '&#x', $sSuffix = ';') 
 ;afan 提示:将 Unicode 编码还原为字符
 ;$sEncode - 编码字符串
 ;$sPrefix - [可选参数]前缀字符
 ;$sSuffix - [可选参数]后缀字符

 Local $Unicode, $i, $S2H, $Us
 $S2H = StringRegExp($sEncode, $sPrefix & '(\w{4})' & $sSuffix, 3)
 If @error Then Return $sEncode
 For $i = 0 To UBound($S2H) - 1
 $Us = ChrW(Dec($S2H[$i])) 
 $Unicode = StringReplace($sEncode, $sPrefix & $S2H[$i] & $sSuffix, $Us, 1)
 $sEncode = $Unicode
 Next
 Return $Unicode
EndFunc ;==>_UnicodeEncodeToString

post数据中的代码转换如:10%E6%9C%883%E6%97%A5 等于10月3日

MsgBox(0, 0, _UnicodeURLEncode('10月3日'))

Func _UnicodeURLEncode($UnicodeURL) ;==>_UnicodeURLEncode
 Local $UnicodeBinaryLength, $UnicodeBinary, $UnicodeBinary2
 $UnicodeBinary = StringToBinary($UnicodeURL, 4)
 $UnicodeBinary2 = StringReplace($UnicodeBinary, '0x', '', 1)
 $UnicodeBinaryLength = StringLen($UnicodeBinary2)
 Local $EncodedString, $UnicodeBinaryChar, $EncodedString
 For $i = 1 To $UnicodeBinaryLength Step 2
 $UnicodeBinaryChar = StringMid($UnicodeBinary2, $i, 2)
 If StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", BinaryToString('0x' & $UnicodeBinaryChar, 4)) Then
 $EncodedString &= BinaryToString('0x' & $UnicodeBinaryChar)
 Else
 $EncodedString &= '%' & $UnicodeBinaryChar
 EndIf
 Next
 Return $EncodedString
EndFunc ;==>_UnicodeURLEncode
永恒心锁,版权所有丨如未注明,均为转载丨本网站采用BY-NC-SA协议进行授权

转载请注明:永恒心锁-分享互联网 » URLEncode_udf[转换字符为ANSI|Unicode UTF 8(16)URL编码及逆转还原]

您必须 登录 才能发表评论!