|
- #If Win64 Then
- Public Declare PtrSafe 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 Long) As Long
- Public Declare PtrSafe Function MultiByteToWideChar Lib "kernel32.dll" (ByVal CodePage As Long, _
- ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
- ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
- #Else
- Public 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 Long) As Long
- Public Declare Function MultiByteToWideChar Lib "kernel32.dll" (ByVal CodePage As Long, _
- ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
- ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
- #End If
- 'GB2312 转换成UTF-8
- Public Function GB2312toUTF8(ByVal str As String) As String
- Dim b() As Byte, i As Long, j As Long, k As Long
- b = StrConv(str, vbFromUnicode)
- ReDim c(UBound(b) * 3) As Byte
- For i = 0 To UBound(b)
- If b(i) < 128 Then
- c(j) = b(i)
- j = j + 1
- Else
- k = WideCharToMultiByte(CP_UTF8, 0, StrConv(ChrW$(b(i)), vbUnicode), -1, ByVal 0&, 0, ByVal 0&, ByVal 0&)
- If k > 2 Then
- WideCharToMultiByte CP_UTF8, 0, StrConv(ChrW$(b(i)), vbUnicode), -1, c(j), k + 1, ByVal 0&, ByVal 0&
- j = j + k - 1
- End If
- End If
- Next i
- GB2312toUTF8 = StrConv(c(), vbUnicode)
- End Function
- 'UTF-8 转换成GB2312
- Public Function UTF8toGB2312(ByVal str As String) As String
- Dim b() As Byte, i As Long, j As Long
- b = StrConv(str & ChrW$(&HFEFF), vbFromUnicode)
- For i = LBound(b) To UBound(b)
- If (b(i) And &H80&) <> &H80& Then
- j = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED Or MB_ERR_INVALID_CHARS, b(i), -1, ByVal lpBuffer&, LenB(lpBuffer))
- ElseIf (b(i) And &HE0&) <> &HE0& Then
- j = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED Or MB_ERR_INVALID_CHARS, CLng((b(i) And &H1F&) * &H10000 Or (b(i + 1) And &H3F&) * &H100& Or (b(i + 2) And &H3F&)), -1, ByVal lpBuffer&, LenB(lpBuffer))
- i = i + 2
- ElseIf (b(i) And &HF0&) <> &HF0& Then
- j = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED Or MB_ERR_INVALID_CHARS, CLng((b(i) And &HF&) * &H40000000 Or (b(i + 1) And &H3F&) * &H10000 Or (b(i + 2) And &H3F&) * &H100& Or (b(i + 3) And &H3F&)), -1, ByVal lpBuffer&, LenB(lpBuffer))
- i = i + 3
- End If
- Next i
- UTF8toGB2312 = StrConv(lpBuffer, j, vbFromUnicode)
- End Function
复制代码 参考 http://www.exceloffice.net/archives/999 |
评分
-
1
查看全部评分
-
|