ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索

文本编码转换

已有 884 次阅读2015-3-16 23:41 |个人分类:文本处理

Function URLDecode(ByVal strIn)
    URLDecode = ""
    Dim sl: sl = 1
    Dim tl: tl = 1
    Dim key: key = "%"
    Dim kl: kl = Len(key)
    sl = InStr(sl, strIn, key, 1)
    Do While sl > 0
        If (tl = 1 And sl <> 1) Or tl < sl Then
            URLDecode = URLDecode & Mid(strIn, tl, sl - tl)
        End If
        Dim hh, hi, hl
        Dim a
        Select Case UCase(Mid(strIn, sl + kl, 1))
            Case "U" 'Unicode URLEncode
                a = Mid(strIn, sl + kl + 1, 4)
                URLDecode = URLDecode & ChrW("&H" & a)
                sl = sl + 6
            Case "E" 'UTF-8 URLEncode
                hh = Mid(strIn, sl + kl, 2)
                a = Int("&H" & hh) 'ascii码
                If Abs(a) < 128 Then
                    sl = sl + 3
                    URLDecode = URLDecode & Chr(a)
                Else
                    hi = Mid(strIn, sl + 3 + kl, 2)
                    hl = Mid(strIn, sl + 6 + kl, 2)
                    a = ("&H" & hh And &HF) * 2 ^ 12 Or ("&H" & hi And &H3F) * 2 ^ 6 Or ("&H" & hl And &H3F)
                    If a < 0 Then a = a + 65536
                    URLDecode = URLDecode & ChrW(a)
                    sl = sl + 9
                End If
            Case Else 'Asc URLEncode
                hh = Mid(strIn, sl + kl, 2) '高位
                a = Int("&H" & hh) 'ascii码
                If Abs(a) < 128 Then
                    sl = sl + 3
                Else
                    hi = Mid(strIn, sl + 3 + kl, 2) '低位
                    a = Int("&H" & hh & hi) '非ascii码
                    sl = sl + 6
                End If
                URLDecode = URLDecode & Chr(a)
        End Select
        tl = sl
        sl = InStr(sl, strIn, key, 1)
    Loop
    URLDecode = URLDecode & Mid(strIn, tl)
End Function

-------------------------------------------------------------------------------
Function ReadUTF(ByVal FileName As String) As String  
    With CreateObject("ADODB.Stream")  
        .Type = 2    '读取文本文件  
        .Mode = 3   '读写  
        .Open  
        .LoadFromFile FileName  
        .Charset = "UTF-8"  '设定编码  
        .Position = 2  
        ReadUTF = .ReadText  
        .Close  
    End With  
End Function 

=====================================
Sub 文本编码转换()
    Dim sFile As String
    Open "C:\filename.txt" For Input As #1
    sFile = StrConv(InputB$(LOF(1), #1), vbUnicode)
    Close #1
End Sub

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 免费注册

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-16 03:51 , Processed in 0.029793 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

返回顶部