8月22015
AU3 - ANSI|Unicode UTF 8(16)URL编码及逆转还原
150625更新:
应求修改_StringToEncode函数,添加一个参数,
以输出形如“2D 4E 87 65 41 00 42 00 43 00 31 00 32 00 33 00”
顺便修改了函数命名
-------------------------by:afan
$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```
<br />
扫描二维码,在手机上阅读
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!