Url地址文字编码转换的问题
我参考了a大的url解码的源码的,想将一段url给解码出来,原url为"qvod%3A%2F%2F890800828%7CE0465DF8A6975D6533327B351A8B7758D29CE6A1%7C%E7%99%BD%E9%B9%BF%E5%8E%9F_White.Deer.Plain_2012_BD.rmvb%7C",正确解码应该是"qvod://890800828|E0465DF8A6975D6533327B351A8B7758D29CE6A1|白鹿原_White.Deer.Plain_2012_BD.rmvb|",但是参照a大发的源码发现解码出来的中文部分都是乱码,求高手解决~
$encode2="qvod%3A%2F%2F890800828%7CE0465DF8A6975D6533327B351A8B7758D29CE6A1%7C%E7%99%BD%E9%B9%BF%E5%8E%9F_White.Deer.Plain_2012_BD.rmvb%7C"
MsgBox(32,"",_AnsiURLEncodeToString($encode2))
Func _AnsiURLEncodeToString($URL_str)
;afan 提示:转换经ANSI(GB2312)URL编码后的字符串为原始字符串
;$URL_str - URL编码字符串
;返回值:成功 - 返回编码字符串的原始字符串; 失败 - 返回原字符串并设置 @Error = 1
Local $Rstr, $aSR, $str_Tmp, $i
$Rstr = StringReplace($URL_str, '+', ' ')
$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], '%', ''))
$Rstr = StringReplace($Rstr, $aSR[$i], $str_Tmp, 1)
Next
Return $Rstr
EndFunc ;==>_AnsiURLEncodeToStringhttp://autoitx.com/forum.php?mod=viewthread&tid=17667&highlight=url
通过论坛搜索到一个能正确解码的帖子,可惜没有源码,本人小白,希望高手帮忙~ 坐等高手出现啊。。。此贴勿沉。。。 哎,自己找到一个帖子解决了。。。#include<array.au3>
Local $cwe="qvod%3A%2F%2F890800828%7CE0465DF8A6975D6533327B351A8B7758D29CE6A1%7C%E7%99%BD%E9%B9%BF%E5%8E%9F_White.Deer.Plain_2012_BD.rmvb%7C"
Local $hhtp=UTFToString($cwe)
MsgBox(0,"",$hhtp)
Func UTFToString($string)
If StringInStr($string,"%")<=0 Then Return $string
Local $ggeer=StringRegExp($string,"(\%[\d\w]{2})",3)
If UBound($ggeer)<=1 Then Return $string
Local $ls_bcbf=""
For $i=0 To UBound($ggeer)-1
$ls_bcbf&=$ggeer[$i]
If $i<>UBound($ggeer)-1 Then
If StringInStr($string,$ls_bcbf&$ggeer[$i+1])=0 Then
$string=StringReplace($string,$ls_bcbf,BinaryToString("0x"&StringReplace($ls_bcbf,"%",""),4),1)
$ls_bcbf=""
EndIf
Else
If $ls_bcbf<>"" Then
$string=StringReplace($string,$ls_bcbf,BinaryToString("0x"&StringReplace($ls_bcbf,"%",""),4),1)
EndIf
EndIf
Next
Return $string
EndFunc 回复 1# hanleicool
编码前的URl为 UTF8 编码,解码用的是 ANSI 解码,当然就乱码了。加个参数传递。
$encode2 = "qvod%3A%2F%2F890800828%7CE0465DF8A6975D6533327B351A8B7758D29CE6A1%7C%E7%99%BD%E9%B9%BF%E5%8E%9F_White.Deer.Plain_2012_BD.rmvb%7C"
MsgBox(32, "", _URLEncodeToString($encode2, 4))
Func _URLEncodeToString($URL_str, $Encode = 1)
;afan 提示:转换经ANSI(GB2312)URL编码后的字符串为原始字符串
;$URL_str - URL编码字符串
;$Encode - URL编码格式 ;1 (默认) = ANSI ;2 = UTF16 小 ;3 = UTF16 大 ;4 = UTF8
;返回值:成功 - 返回编码字符串的原始字符串; 失败 - 返回原字符串并设置 @Error = 1
Local $Rstr, $aSR, $str_Tmp, $i
$Rstr = StringReplace($URL_str, '+', ' ')
$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 ;==>_URLEncodeToString 回复 4# afan
厉害厉害!又学习了,非常感谢!
页:
[1]