設為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊

QQ登錄

只需一步,快速開始

返回列表 發(fā)新帖
查看: 5070|回復: 2
打印 上一主題 下一主題

[模塊/函數(shù)] 二進制UTF8l轉成字符串代碼

[復制鏈接]

點擊這里給我發(fā)消息

跳轉到指定樓層
1#
發(fā)表于 2013-7-21 14:39:43 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
最近在處理二進制UTF8編碼,分享一個函數(shù)

Private Declare Function CryptStringToBinary Lib "Crypt32" _
    Alias "CryptStringToBinaryW" ( _
    ByVal pszString As Long, _
    ByVal cchString As Long, _
    ByVal dwFlags As Long, _
    ByVal pbBinary As Long, _
    ByRef pcbBinary As Long, _
    ByRef pdwSkip As Long, _
    ByRef pdwFlags As Long) As Long

Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
    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

Public Function FromHex(ByRef HexString As String) As Byte()
    Const CRYPT_STRING_HEX As Long = &H4&
    Dim lngOutLen As Long
    Dim dwActualUsed As Long
    Dim bytBinary() As Byte

    If Len(HexString) < 1 Then Exit Function

    'Determine output buffer length required.
    If CryptStringToBinary(StrPtr(HexString), _
                           Len(HexString), _
                           CRYPT_STRING_HEX, _
                           0&, _
                           lngOutLen, _
                           ByVal 0&, _
                           dwActualUsed) = 0 Then
        Err.Raise &H80044100, "FromHex", _
                  "CryptStringToBinary failed, error " & CStr(Err.LastDllError)
    Else
        'Convert to binary.
        ReDim bytBinary(lngOutLen - 1)
        If CryptStringToBinary(StrPtr(HexString), _
                               Len(HexString), _
                               CRYPT_STRING_HEX, _
                               VarPtr(bytBinary(0)), _
                               lngOutLen, _
                               ByVal 0&, _
                               dwActualUsed) = 0 Then
            Err.Raise &H80044100, "FromHex", _
                      "CryptStringToBinary failed, error " & CStr(Err.LastDllError)
        Else
            FromHex = bytBinary
        End If
    End If
End Function

Public Function FromUtf8(ByRef Utf8() As Byte) As String
    Const CP_UTF8 As Long = 65001
    Dim lngBytes As Long
    Dim lngResult As Long

    On Error Resume Next
    lngBytes = UBound(Utf8) - LBound(Utf8) + 1
    If Err Then
        Err.Clear
        On Error GoTo 0
        Err.Raise 5, "FromUtf8", "Invalid parameter: must be a dimensioned array"
    End If
    On Error GoTo 0
    lngResult = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf8(LBound(Utf8))), _
                                    lngBytes, 0, 0)
    FromUtf8 = String$(lngResult, 0)
    MultiByteToWideChar CP_UTF8, 0, VarPtr(Utf8(LBound(Utf8))), _
                        lngBytes, StrPtr(FromUtf8), lngResult
End Function

Private Sub cmdConvert_Click()
    fm20TxtText.Text = FromUtf8(FromHex(fm20TxtHex.Text))
End Sub

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享分享 分享淘帖 訂閱訂閱
2#
發(fā)表于 2014-5-13 22:34:13 | 只看該作者
好好學習,天天向上

點擊這里給我發(fā)消息

3#
發(fā)表于 2014-8-16 05:32:57 來自手機 | 只看該作者
不錯,用ADO,stream也可以吧?
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則

QQ|站長郵箱|小黑屋|手機版|Office中國/Access中國 ( 粵ICP備10043721號-1 )  

GMT+8, 2024-10-23 10:25 , Processed in 0.097715 second(s), 30 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復 返回頂部 返回列表