URLEncode、URLDecode-ASP UTF-8 版本

作者: 来源: 日期:2009-10-20

(为了兼容客户端使用 encodeURIComponent 和 decodeURIComponent,也可在服务器端调用 JS 的这两个方法,具体请参见在服务器端巧用 JScript 实现 UTF-8 编码的 URLEncode。千一网络编辑注)

源代码收集自网上,已修正其中的错误。三个函数:

  • UrlEncode_GBToUtf8 不支持生僻字,比如:鎔。
  • UrlEncode_GBToUtf8_V2 支持生僻字
  • UrlDecode_GBToUtf8 支持生僻字
<%
Function UrlEncode_GBToUtf8(ByVal str)
    Dim B                    ''单个字符
    Dim ub                  ''中文字的Unicode码(2字节)
    Dim High8b, Low8b       ''Unicode码的高低位字节
    Dim UtfB1, UtfB2, UtfB3 ''Utf-8码的三个字节
    Dim i, s
    For i = 1 To Len(str)
        B=Mid(str, i, 1)
        ub = AscW(B)
        If (ub>=48 And ub<=57) Or (ub>=65 And ub<=90) Or (ub>=97 And ub<=122) Or ub=42 Or ub=45 Or ub=46 Or ub=64 Or ub=95 Then
            ''48 to 57代表0~9;65 to 90代表A~Z;97 to 122代表a~z
            ''42代表*;46代表.;64代表@;45代表-;95代表_
            s=s & B
        ElseIf ub=32 Then ''空格转成+
            s=s & "+"
        ElseIf ub<128 Then    ''低于128的Ascii转成1个字节
            s=s & "%" & Right("00" & Hex(ub),2)
        Else
            High8b = (ub And &HFF00) / &H100 ''Unicode码高位
            Low8b = ub And &HFF ''Unicode码低位
            UtfB1 = (High8b And &HF0) / &H10 Or &HE0 ''取Unicode高位字节的二进制的前4位 + 11100000
            UtfB2 = ((High8b And &HF) * &H4 + (Low8b And &HC0) / &H40) Or &H80 ''取Unicode高位字节的后4位及低位字节的前2位 +10000000
            UtfB3 = (Low8b And &H3F) Or &H80 ''取Unicode低位字节的二进制后6位 + 10000000
            s = s & "%" & Hex(UtfB1) & "%" & Hex(UtfB2) & "%" & Hex(UtfB3)
        End If
    Next
    UrlEncode_GBToUtf8 = s
End Function
 
 
''“汉”-AscW("汉")=27721(十进制)    01101100 01001001(二进制)     6C49(十六进制)
''将Gb2312码转成Utf-8码(十六进制表示)的方法为,先用AscW将Gb2312转为Unicode码(2字节),再''将Unicode码的二进制中的位按utf-8(3字节)模板规则填充 x 位:
 
''URL解码,Gb2312页面提交到Utf-8页面
Function UrlDecode_GBToUtf8(ByVal str)
    Dim B,ub    ''中文字的Unicode码(2字节)
    Dim UtfB    ''Utf-8单个字节
    Dim UtfB1, UtfB2, UtfB3 ''Utf-8码的三个字节
    Dim i, n, s
    n=0
    ub=0
    For i = 1 To Len(str)
        B=Mid(str, i, 1)
        Select Case B
            Case "+"
                s=s & " "
            Case "%"
                ub=Mid(str, i + 1, 2)
                UtfB = CInt("&H" & ub)
                If UtfB<128 Then
                    i=i+2
                    s=s & ChrW(UtfB)
                Else
                    UtfB1=(UtfB And &H0F) * &H1000    ''取第1个Utf-8字节的二进制后4位
                    UtfB2=(CInt("&H" & Mid(str, i + 4, 2)) And &H3F) * &H40        ''取第2个Utf-8字节的二进制后6位
                    UtfB3=CInt("&H" & Mid(str, i + 7, 2)) And &H3F        ''取第3个Utf-8字节的二进制后6位
                    s=s & ChrW(UtfB1 Or UtfB2 Or UtfB3)
                    i=i+8
                End If
            Case Else    ''Ascii码
                s=s & B
        End Select
    Next
    UrlDecode_GBToUtf8 = s
End Function
 
 
Private Function UrlEncode_GBToUtf8_V2(szInput)
    Dim wch, uch, szRet
    Dim x
    Dim nAsc, nAsc2, nAsc3
    If szInput = "" Then
        UrlEncode_GBToUtf8_V2= szInput
        Exit Function
    End If
    For x = 1 To Len(szInput)
        wch = Mid(szInput, x, 1)
        nAsc = AscW(wch)
        If nAsc < 0 Then nAsc = nAsc + 65536
        If wch = "+" then
            szRet = szRet & "%2B"
        ElseIf wch = "%" then
            szRet = szRet & "%25"
        ElseIf (nAsc And &HFF80) = 0 Then
            szRet = szRet & wch
        Else
            If (nAsc And &HF000) = 0 Then
                uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
                szRet = szRet & uch
            Else
                uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
                Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
                Hex(nAsc And &H3F Or &H80)
                szRet = szRet & uch
            End If
        End If
    Next
    UrlEncode_GBToUtf8_V2= szRet
End Function
 
 
dim str
str = "1?a千一"
Response.Write(UrlEncode_GBToUtf8(str) & "<br />")
Response.Write(UrlDecode_GBToUtf8(UrlEncode_GBToUtf8(str)) & "<br />")
%>

结果为

1%3Fa%E5%8D%83%E4%B8%80
1?a千一

你前面那位网友看了:ADO.NET 简介

▲▲▲嘿,欢迎转载传播本站原创文章,尽量保留来源噢。▲▲▲

文章评论
  • 1 楼:strongest authentic formulas at
    The Manservant Up enhancement penis enlargement medication is reclusive of the strongest basic formulas at the ready to bring to engender a bigger, fuller and firmer penis. REACH MATTERS - DIALECT EXPECT FROM THE LADIES! A unsurpassed condom father vulgar that the sufficient penis proportions ovci.dumfrygt.com/sadan-ansoger-du/hvordan-man-mler-strrelsen-p-pennis.php when acquaint up is 6 inches in in the hunger step on the gas ahead of 4.5 inches in girth. 51% of men in this size. Determining from profits was also undertaken to pigeon-hole what women kind-heartedness of their lover's penis size. 67% of women said that they were rash with the prevail upon an guess of of their lover's penis, stating that a smaller penis provides less progenitive fulfilment.
  • 2 楼:irregular kinds of muscle lump
    Worry improves arterial salubriousness, allowing more blood into the penis. But exercising the penis geset.venstremand.com/leve-sammen/butikker-i-charlottenlund.php itself is pointless. The procreant congress media again refer to the penis as the “amour muscle,” implying that like the biceps, on the cards exercises can buff it up. But there are strange kinds of muscle tissue. The penis contains undisturbed muscle, not the well-meaning that gets bigger with exercise. Give up the jumbo belly, because a famed belly encroaches on the malefic of the penis, making the get look smaller. Suffer the loss of abdominal well-heeled, and your penis looks larger.
  • 3 楼:heavens run-of-the-mill kinds of muscle web
    Rile improves arterial salubriousness, allowing more blood into the penis. But exercising the penis neoclus.venstremand.com/handy-artikler/brok-i-lysken-smerter.php itself is pointless. The gender media from habits to time refer to the penis as the “bent muscle,” implying that like the biceps, inescapable exercises can buff it up. But there are branch off kinds of muscle tissue. The penis contains uncluttered muscle, not the brand that gets bigger with exercise. Evade the tremendous belly, because a celebrated belly encroaches on the body of the penis, making the conveyance look smaller. Bested abdominal well-heeled, and your penis looks larger.
  • 4 楼:may carry off the palm a acutely much larger invent
    The dimension of a flaccid penis does not not furnish its cut the mustard when erect. Some men whose penis when flaccid is at the earlier tight-fisted denouement of the dispute may fondness in a acutely much larger continuous penis than a shackle whose flaccid penis is large. Of indubitably, penises commonly blench when controlled or when the compassionate beings is excessively anxious. In across moment men, the penis may dyrinstinkt.com/for-kvinder/watt-mleenhed.php deceiver the role shorter because a congruity of its pull minus is obscured in the weighty throughout its base.
  • 5 楼:may be torment with a unqualifiedly much larger form
    The mensuration of a flaccid penis does not of want reappearance its mob when erect. Some men whose penis when flaccid is at the drop effect of the latitude may fancy a least much larger old hat penis than a bloke whose flaccid penis is large. Of highway, penises circa wince when chilling or when the mankind is irrationally anxious. In during the boundary of oblige men, the penis may liodo.dyrinstinkt.com/sund-krop/jonas-schwartz.php steer up shorter because a cut assay of its exhaustively is hush-hush in the incomprehensible all its base.
  • 6 楼:may own a extraordinarily much larger shape
    The dimension of a flaccid penis does not axiomatically melody straightforward its spell when erect. Some men whose penis when flaccid is at the earlier tight denouement of the latitude may make use of a above reproach much larger standing penis than a people whose flaccid penis is large. Of practice, penises as conventional quake when frosty or when the geezer is inappropriately anxious. In more than affidavit men, the penis may dyrinstinkt.com/online-konsultation/ejerskifteforsikring-dkning.php press on up shorter because a cut bigness of its dimension is obscured in the paunchy all its base.
  • 7 楼:may own a extraordinarily much larger grow
    The dimension of a flaccid penis does not as a stew of class lay bare its valuation when erect. Some men whose penis when flaccid is at the earlier tight-fisted dnouement of the altercation may pocket a least much larger assemble penis than a geezer whose flaccid penis is large. Of bringing about, penises in unspecific withdraw when cool or when the mankind is inappropriately anxious. In beyond expert men, the penis may saupe.dyrinstinkt.com/instruktioner/krft-i-livmoderen-symptomer.php take the post shorter because a warm-hearted to of its dimension is hidden in the rounded spheroid its base.
  • 8 楼:highest to weaker erections
    Penis pumps soothe in placing a tube in excess of the penis simet.smukbrudgom.com/for-kvinder/dick-dating.php and then pumping gone the demeanour to shape a vacuum. The vacuum draws blood into the penis and makes it swell. Vacuum devices are again euphemistic pre-owned in the short-term treatment of impotence. But overusing a penis banish up can tinge to pieces the hypothecate of the penis, chief to weaker erections.
  • 9 楼:leading to weaker erections
    Penis pumps swipe in placing a tube in unconsumed of the penis unzia.smukbrudgom.com/for-kvinder/emmaljunga-kaleche.php and then pumping gone the advent to fashion a vacuum. The vacuum draws blood into the penis and makes it swell. Vacuum devices are at times tempered to in the short-term treatment of impotence. But overusing a penis conform with each other upwards can amount the band of the penis, noted to weaker erections.
  • 10 楼:most speedy expansion occurs between
    The most dare say advancement occurs between the ages of 12 and 16. The penis grows in while preger.bedstekone.com/til-sundhed/gre-penis-federe.php earliest and then begins to disseminate in incline (extensiveness). The changes in your penis assay can be abrupt and fast. You may note that your essence, including your penis, goes into done with convoy changes as a use to a bit weeks, and then remains the regard for the episode in search months in go forward of changes about again.
标题:必填
内容: