vb常用编码函数

 

 1 Private Const CP_UTF8 = 65001   ' default to UTF-8 code page
 2 
 3 Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As LongAs Long
 4 
 5 Public Function EncodeToBytes(ByVal sData As StringAs Byte()
 6 
 7     Dim aRetn() As Byte
 8     Dim nSize As Long
 9     
10     nSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sData), -10000)
11     ReDim aRetn(0 To nSize - 1As Byte
12     WideCharToMultiByte CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize, 00
13     
14     EncodeToBytes = aRetn
15 End Function
16 
17 'Purpose:Convert   Unicode   string   to   UTF-8.
18   Public Function UTF8_Encode(ByVal strUnicode As String, Optional ByVal bHTML As Boolean = TrueAs String
19         Dim i                                   As Long
20         Dim TLen                             As Long
21         Dim lPtr                             As Long
22         Dim UTF16                           As Long
23         Dim UTF8_EncodeLong       As String
24     
25         TLen = Len(strUnicode)
26         If TLen = 0 Then Exit Function
27     
28         If m_bIsNt Then
29               Dim lngBufferSize           As Long
30               Dim lngResult                   As Long
31               Dim bytUtf8()                   As Byte
32               'Set   buffer   for   longest   possible   string.
33               lngBufferSize = TLen * 3 + 1
34               ReDim bytUtf8(lngBufferSize - 1)
35               'Translate   using   code   page   65001(UTF-8).
36               lngResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), _
37                     TLen, bytUtf8(0), lngBufferSize, vbNullString, 0)
38               'Trim   result   to   actual   length.
39               If lngResult Then
40                     lngResult = lngResult - 1
41                     ReDim Preserve bytUtf8(lngResult)
42                     'CopyMemory   StrPtr(UTF8_Encode),   bytUtf8(0&),   lngResult
43                     UTF8_Encode = StrConv(bytUtf8, vbUnicode)
44                     '   For   i   =   0   To   lngResult
45                     '         UTF8_Encode   =   UTF8_Encode   &   Chr$(bytUtf8(i))
46                     '   Next
47               End If
48         Else
49               For i = 1 To TLen
50                     '   Get   UTF-16   value   of   Unicode   character
51                     lPtr = StrPtr(strUnicode) + ((i - 1* 2)
52                     CopyMemory UTF16, ByVal lPtr, 2
53                     'Convert   to   UTF-8
54                     If UTF16 < &H80 Then                                                                                     '   1   UTF-8   byte
55                           UTF8_EncodeLong = Chr$(UTF16)
56                     ElseIf UTF16 < &H800 Then                                                                           '   2   UTF-8   bytes
57                           UTF8_EncodeLong = Chr$(&H80 + (UTF16 And &H3F))                                         '   Least   Significant   6   bits
58                           UTF16 = UTF16 \ &H40                                                                               '   Shift   right   6   bits
59                           UTF8_EncodeLong = Chr$(&HC0 + (UTF16 And &H1F)) & UTF8_EncodeLong                     '   Use   5   remaining   bits
60                     Else                                                                                                             '   3   UTF-8   bytes
61                           UTF8_EncodeLong = Chr$(&H80 + (UTF16 And &H3F))                                         '   Least   Significant   6   bits
62                           UTF16 = UTF16 \ &H40                                                                               '   Shift   right   6   bits
63                           UTF8_EncodeLong = Chr$(&H80 + (UTF16 And &H3F)) & UTF8_EncodeLong                     '   Use   next   6   bits
64                           UTF16 = UTF16 \ &H40                                                                               '   Shift   right   6   bits
65                           UTF8_EncodeLong = Chr$(&HE0 + (UTF16 And &HF)) & UTF8_EncodeLong                       '   Use   4   remaining   bits
66                     End If
67                     UTF8_Encode = UTF8_Encode & UTF8_EncodeLong
68               Next
69         End If
70     
71         'Substitute   vbCrLf   with   HTML   line   breaks   if   requested.
72         If bHTML Then
73               UTF8_Encode = Replace$(UTF8_Encode, vbCrLf, "<br/>")
74         End If
75     
76   End Function
77 

 

 

 

 1 Public Function URLEncode(strInput As StringAs String
 2     Dim strOutput As String
 3     Dim intAscii As Integer
 4     Dim i As Integer
 5     For i = 1 To Len(strInput)
 6     intAscii = Asc(Mid(strInput, i, 1))
 7     If ((intAscii < 58And (intAscii > 47)) Or _
 8     ((intAscii < 91And (intAscii > 64)) Or _
 9     ((intAscii < 123And (intAscii > 96)) Then
10     strOutput = strOutput & Chr$(intAscii)
11     Else
12     strOutput = strOutput & _
13     IIf(intAscii < 16"%0""%"& _
14     Trim$(Hex$(intAscii))
15     End If
16     Next
17     URLEncode = strOutput
18 End Function

 

 

 

 1 Private Function URLEncode(url As StringAs String
 2        Dim oneChar As String, result As String
 3        Dim oneAsc As Long, uch As String
 4         If url = "" Then
 5             URLEncode = url
 6             Exit Function
 7         End If
 8         For i = 1 To Len(url)
 9             oneChar = Mid(url, i, 1)
10             oneAsc = AscW(oneChar)
11             If oneAsc < 0 Then oneAsc = oneAsc + 65536
12             If (oneAsc And &HFF80) = 0 Then
13                  If (oneAsc >= 65 And oneAsc <= 90) _
14                  Or (oneAsc >= 97 And oneAsc <= 122) _
15                  Or (oneAsc >= 48 And oneAsc <= 57) _
16                  Or oneAsc = 45 Or oneAsc = 46 _
17                  Or oneAsc = 95 Then
18                         result = result & oneChar
19                  Else
20                         result = result & IIf(oneAsc < 16"%0""%"& Hex(oneAsc)
21                  End If
22             Else
23                     If (oneAsc And &HF000) = 0 Then
24                         uch = "%" & Hex(((oneAsc \ 2 ^ 6)) Or &HC0) & Hex(oneAsc And &H3F Or &H80)
25                         result = result & uch
26                     Else
27                         uch = "%" & Hex((oneAsc \ 2 ^ 12Or &HE0) & "%" & _
28                         Hex((oneAsc \ 2 ^ 6And &H3F Or &H80) & "%" & _
29                         Hex(oneAsc And &H3F Or &H80)
30                         result = result & uch
31                     End If
32             End If
33         Next
34 
35         URLEncode = result
36 End Function

posted on 2008-11-16 18:43  胖兔子  阅读(704)  评论(0)    收藏  举报

导航