隨筆 - 251  文章 - 504  trackbacks - 0
          <2007年11月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          本博客系個人收集材料及學習記錄之用,各類“大俠”勿擾!

          留言簿(14)

          隨筆分類

          收藏夾

          My Favorite Web Sites

          名Bloger

          非著名Bloger

          搜索

          •  

          積分與排名

          • 積分 - 204286
          • 排名 - 283

          最新評論

              在使用Google或者百度進行中文搜索的時候,我們會發現搜索過程中URL地址欄會變成一串格式字符串編碼,我們編寫程序經常也需要進行中文漢字到這類URL編碼的轉換,這里介紹兩段相關的轉換函數。

              對于使用UTF-8的Google搜索引擎來說,使用Google搜索“漢字”會變成http://www.google.com/search?q= %E6%B1%89%E5%AD%97 ,而對于使用GB2312的百度搜索引擎來說,使用百度搜索“漢字”會變成另外的 http://www.baidu.com/s?wd=%BA%BA%D7%D6 。下面的兩段VB代碼分別針對UTF-8(UTF8EncodeURI)和GB2312(GBKEncodeURI)進行了編碼的轉換。

          Private Sub command1_click()
                  Debug.Print (UTF8EncodeURI(
          "漢字"))
                  Debug.Print (GBKEncodeURI(
          "漢字"))
              End Sub


              Function UTF8EncodeURI(szInput)
                  Dim wch, uch, szRet
                  Dim x
                  Dim nAsc, nAsc2, nAsc3

                  If szInput 
          = "" Then
                      UTF8EncodeURI 
          = 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 (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

                  UTF8EncodeURI 
          = szRet
              End Function

              Function GBKEncodeURI(szInput)
                  Dim i As Long
                  Dim x() As Byte
                  Dim szRet As String

                  szRet 
          = ""
                  x 
          = StrConv(szInput, vbFromUnicode)
                  For i 
          = LBound(x) To UBound(x)
                      szRet 
          = szRet & "%" & Hex(x(i))
                  Next
                  GBKEncodeURI 
          = szRet
              End Function


          轉載自月光博客 [ http://www.williamlong.info/ ]
          posted on 2007-11-20 00:53 matthew 閱讀(796) 評論(0)  編輯  收藏 所屬分類: JavaEE
          主站蜘蛛池模板: 巴中市| 平阳县| 手游| 铜山县| 黄陵县| 信宜市| 米泉市| 海阳市| 社旗县| 德州市| 聂拉木县| 七台河市| 汝州市| 晋宁县| 扶沟县| 安仁县| 隆回县| 信宜市| 永吉县| 嘉峪关市| 桂林市| 嘉荫县| 延川县| 陆河县| 平顺县| 大理市| 和龙市| 拉萨市| 岢岚县| 株洲县| 星座| 衡阳市| 木里| 贵溪市| 德格县| 宁陕县| 高碑店市| 永康市| 弋阳县| 集安市| 巴林右旗|