之前有问如何实现 UNICODE编码见
http://topic.csdn.net/u/20090331/14/e849fef1-cd72-4941-8ae2-98b8fabff299.html
Public Function URLEncode(ByRef strURL As String) As String
Dim I As Long
Dim tempStr As String
For I = 1 To Len(strURL)
If Asc(Mid(strURL, I, 1)) < 0 Then
tempStr = "%" & Right(CStr(Hex(Asc(Mid(strURL, I, 1)))), 2)
tempStr = "%" & Left(CStr(Hex(Asc(Mid(strURL, I, 1)))), Len(CStr(Hex(Asc(Mid(strURL, I, 1))))) - 2) & tempStr
URLEncode = URLEncode & tempStr
ElseIf (Asc(Mid(strURL, I, 1)) >= 65 And Asc(Mid(strURL, I, 1)) <= 90) Or (Asc(Mid(strURL, I, 1)) >= 97 And Asc(Mid(strURL, I, 1)) <= 122) Then
URLEncode = URLEncode & Mid(strURL, I, 1)
Else
URLEncode = URLEncode & "%" & Hex(Asc(Mid(strURL, I, 1)))
End If
Next
End Function
可实现但是反 UNICODE编码
Public Function URLDecode(ByRef strURL As String) As String
Dim I As LongIf InStr(strURL, "%") = 0 Then URLDecode = strURL: Exit FunctionFor I = 1 To Len(strURL)
If Mid(strURL, I, 1) = "%" Then
If Val("&H" & Mid(strURL, I + 1, 2)) > 127 Then
URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2) & Mid(strURL, I + 4, 2)))
I = I + 5
Else
URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2)))
I = I + 2
End If
Else
URLDecode = URLDecode & Mid(strURL, I, 1)
End If
Next
End Function
好像有问题比如你好的 UNICODE 为 %E4%BD%A0%E5%A5%BD但是这个函数 后的结果是什么 涴 什么 乱码的该如何解决这个问题?

解决方案 »

  1.   

    1、初步确认:是你编码程序错误,而不是你的解码程序错误。你给的测试编码本身就是错的。
    2、编码不是Unicode,编码是GBK的HEX形式。
    3、造成你编码程序错误的原因是Integer类型的符号位问题。
    Private Sub Form_Load()
      Dim tByte() As Byte
      Debug.Print DeCode("%D0%A1%CF%C9%C3%C3%CA%C7%B8%F6%BA%C3%BA%A2%D7%D3")
    End SubPublic Function EnCode(ByVal pString As String) As String
      '编码函数  Dim tOutString As String
      Dim tSurBytes() As Byte
      Dim tSurBytes_Len As Long
      Dim tSurBytes_Index As Long
      Dim tDesStrs() As String
      Dim tDesStrs_Len As Long
      Dim tDesStrs_Index As Long
      
      tSurBytes() = StrConv(pString, vbFromUnicode)
      tSurBytes_Len = UBound(tSurBytes())
      
      tDesStrs_Len = tSurBytes_Len + 1
      ReDim tDesStrs(tDesStrs_Len)
      
      For tSurBytes_Index = 0 To tSurBytes_Len
        tDesStrs_Index = tSurBytes_Index + 1
        tDesStrs(tDesStrs_Index) = Hex(tSurBytes(tSurBytes_Index))
      Next
      
      tOutString = Join(tDesStrs(), "%")
      EnCode = tOutString
      
    End FunctionPublic Function DeCode(ByVal pString As String) As String
      '解码函数
      
      Dim tOutString As String
      
      Dim tSurStrs() As String
      Dim tSurStrs_Len As Long
      Dim tSurStrs_Index As Long
      
      Dim tDesBytes() As Byte
      Dim tDesBytes_Len As Long
      Dim tDesBytes_Index As Long
      
      tSurStrs() = Split(pString, "%")
      tSurStrs_Len = UBound(tSurStrs())
      
      tDesBytes_Len = tSurStrs_Len - 1
      
      ReDim tDesBytes(tDesBytes_Len)
      
      For tDesBytes_Index = 0 To tDesBytes_Len
        
        tSurStrs_Index = tDesBytes_Index + 1
        tDesBytes(tDesBytes_Index) = CByte("&H" & tSurStrs(tSurStrs_Index))
          
      Next
      
      tOutString = StrConv(tDesBytes(), vbUnicode)
      
      DeCode = tOutString
    End Function
      

  2.   

    LS的这个编码好像跟浏览器的URL编码并不同你可以打开
    http://www.google.cn/
    输入汉字看看所以好像并不是我想要的
      

  3.   


    我是参照你在这个帖子里说的百度的编码做的。
    http://topic.csdn.net/u/20090331/14/e849fef1-cd72-4941-8ae2-98b8fabff299.html百度是GB2312编码,Google是UTF8编码。UTF8做起来就麻烦多了,可不是你想象中那么简单的。
      

  4.   

    UTF8的编码规则是这样的:1、基于Unicode编码。
    2、做如下转换:U-00000000 - U-0000007F: 0xxxxxxx  
    U-00000080 - U-000007FF: 110xxxxx 10xxxxxx  
    U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx  
    U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx  
    U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx  
    U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx这是Linux下那帮王八蛋搞出来的操蛋规则,只能用状态机来解读,不能直接线性转换。
      

  5.   

    原来如此不过GG好像也支持 
    GB2312那GG 的是不是没办法反编码了?
      

  6.   


    可以互相转换,但解码程序复杂多了。我有闲功夫的话倒是可以写出来,不过那代码你看起来会晕的。汉字编码基本是这样:
    1110xxxx 10xxxxxx 10xxxxx那么解码程序是这样的:1、三个为一组。
    2、取第一个输入编码的末4位:OBH = InByte1 and &HF
    3、作为输出编码第一个的高4位:OutByte1 = OBH * &H10
    4、取第二个输入编码的5-2位:OBL = (InByte2 And &H3C) \ &H10
    3、作为输出编码第一个的低4位:OutByte1 = OutByte + OBL
    5、取第二个输入编码的1-0位:OBH = (InByte2 And &H3)
    6、作为输出编码第二个的高2位:OutByte2 = OBH * &H40
    7、取第三个输入编码的低6位:OBL = (InByte3 And &H3F)
    8、作为输出编码第二个的低6位:OutByte2 = OutByte2 + OBL
    9、接下来是把字节变成字符串……现在你应该知道UTF8是多么讨厌一种编码了吧?
      

  7.   

    我测试了
    原来BAIDU 里 输入"你好"  转化后为 "%C4%E3%BA%C3"Private Function URLDecode(ByRef strURL As String) As String
    Dim i As LongIf InStr(strURL, "%") = 0 Then URLDecode = strURL: Exit FunctionFor i = 1 To Len(strURL)
    If Mid(strURL, i, 1) = "%" Then
    If Val("&H" & Mid(strURL, i + 1, 2)) > 127 Then
    URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, i + 1, 2) & Mid(strURL, i + 4, 2)))
    i = i + 5
    Else
    URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, i + 1, 2)))
    i = i + 2
    End If
    Else
    URLDecode = URLDecode & Mid(strURL, i, 1)
    End If
    Next
    End Function
    这个代码是有效果的关键是 google 里
    输入"你好"  转化后为 %E4%BD%A0%E5%A5%BD"要反编码有难度啊