是要将utf-8转换为unicode吧?是的话,参考: 'Utf8 转换为 Unicode Public Function UTF8_Decode(ByVal s As String) As String Dim lUtf8Size As Long Dim sBuffer As String Dim lBufferSize As Long Dim lResult As Long Dim b() As Byte If LenB(s) Then On Error GoTo EndFunction b = StrConv(s, vbFromUnicode) lUtf8Size = UBound(b) + 1 On Error GoTo 0 'Set buffer for longest possible string i.e. each byte is 'ANSI<=&HFF, thus 1 unicode(2 bytes)for every utf-8 character. lBufferSize = lUtf8Size * 2 sBuffer = String$(lBufferSize, vbNullChar) 'Translate using code page 65001(UTF-8) lResult = MultiByteToWideChar(CP_UTF8, 0, b(0), _ lUtf8Size, StrPtr(sBuffer), lBufferSize) 'Trim result to actual length If lResult Then UTF8_Decode = Left$(sBuffer, lResult) 'Debug.Print UTF8_Decode End If End IfEndFunction:End Function 'Unicode转换为UTF-8. Public Function UTF8_Encode(ByVal strUnicode As String) As String Dim TLen As Long TLen = Len(strUnicode) If TLen = 0 Then Exit Function Dim lBufferSize As Long Dim lResult As Long Dim b() As Byte 'Set buffer for longest possible string. lBufferSize = TLen * 3 + 1 ReDim b(lBufferSize - 1) 'Translate using code page 65001(UTF-8). lResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), _ TLen, b(0), lBufferSize, vbNullString, 0) 'Trim result to actual length. If lResult Then lResult = lResult - 1 ReDim Preserve b(lResult) UTF8_Encode = StrConv(b, vbUnicode) End IfEnd Function
Dim b() As Byte
Dim s As String
s = "AB"
b = s
Debug.Print b(0), b(1), b(2), b(3)
End Sub
另外我真的很想知道,为什么不用StrConv ?
ChrW 返回包含指定 Unicode 字符代码的字符串。 ChrW (AscW("h"))
Chice_wxg(学)(习)
不用的原因是因为所有汉字的 UTF-8 都是3个字节,只有 utf-16 才是2个字节,StrConv 只支持 utf-16,不支持 utf-8
flyingscv(zlj)
拜托,我说了不能用这个了啊
'Utf8 转换为 Unicode
Public Function UTF8_Decode(ByVal s As String) As String Dim lUtf8Size As Long
Dim sBuffer As String
Dim lBufferSize As Long
Dim lResult As Long
Dim b() As Byte If LenB(s) Then
On Error GoTo EndFunction
b = StrConv(s, vbFromUnicode)
lUtf8Size = UBound(b) + 1
On Error GoTo 0
'Set buffer for longest possible string i.e. each byte is
'ANSI<=&HFF, thus 1 unicode(2 bytes)for every utf-8 character.
lBufferSize = lUtf8Size * 2
sBuffer = String$(lBufferSize, vbNullChar)
'Translate using code page 65001(UTF-8)
lResult = MultiByteToWideChar(CP_UTF8, 0, b(0), _
lUtf8Size, StrPtr(sBuffer), lBufferSize)
'Trim result to actual length
If lResult Then
UTF8_Decode = Left$(sBuffer, lResult)
'Debug.Print UTF8_Decode
End If
End IfEndFunction:End Function
'Unicode转换为UTF-8.
Public Function UTF8_Encode(ByVal strUnicode As String) As String
Dim TLen As Long TLen = Len(strUnicode)
If TLen = 0 Then Exit Function Dim lBufferSize As Long
Dim lResult As Long
Dim b() As Byte
'Set buffer for longest possible string.
lBufferSize = TLen * 3 + 1
ReDim b(lBufferSize - 1)
'Translate using code page 65001(UTF-8).
lResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), _
TLen, b(0), lBufferSize, vbNullString, 0)
'Trim result to actual length.
If lResult Then
lResult = lResult - 1
ReDim Preserve b(lResult)
UTF8_Encode = StrConv(b, vbUnicode)
End IfEnd Function
原来如此。那么我的方法就别用了。对汉字编换出来不是Unicode编码 ^_^
?????????????????????????????????ByVal strUnicode As String不是要用 Byte 吗?
不是要用 Byte 吗?最好用byte,其实上面的这段是一个老外写的,我没细看//另外,最后还是用这个来转换 UTF-8 的
UTF8_Encode = StrConv(b, vbUnicode)这个不用StrConv一样可以实现
UTF8_Encode = StrConv(b, vbUnicode)这个不用StrConv一样可以实现我就是要不用 StrConv 实现的代码啊!!!
这是将ansi转化为unicode,你确定是需要这个吗?
http://www.netpadd.com/source.php?topic=modInCodePage.bas