如果用 记录本保存的话,utf-8编码的头三个字节是: &HEF、&HBB、&HBF如果直接以byte()数组写入文件,是不好判断的。以下两个函数可以进行 utf-8 编码与解码,在win98中无效。Private Function UTF8_Encode(ByVal strUnicode As String) As Byte() 'UTF-8 编码 Dim TLen As Long Dim lngBufferSize As Long Dim lngResult As Long Dim bytUtf8() As Byte
TLen = Len(strUnicode) If TLen = 0 Then Exit Function
If lngResult <> 0 Then lngResult = lngResult - 1 ReDim Preserve bytUtf8(lngResult) End If
UTF8_Encode = bytUtf8 End FunctionPrivate Function UTF8_Decode(ByRef bUTF8() As Byte) As String 'UTF-8 解码 Dim lRet As Long Dim lLen As Long Dim lBufferSize As Long Dim sBuffer As String Dim bBuffer() As Byte
'UTF-8 编码 Dim TLen As Long
Dim lngBufferSize As Long
Dim lngResult As Long
Dim bytUtf8() As Byte
TLen = Len(strUnicode)
If TLen = 0 Then Exit Function
lngBufferSize = TLen * 3 + 1
ReDim bytUtf8(lngBufferSize - 1)
lngResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), TLen, bytUtf8(0), lngBufferSize, vbNullString, 0)
If lngResult <> 0 Then
lngResult = lngResult - 1
ReDim Preserve bytUtf8(lngResult)
End If
UTF8_Encode = bytUtf8
End FunctionPrivate Function UTF8_Decode(ByRef bUTF8() As Byte) As String
'UTF-8 解码
Dim lRet As Long
Dim lLen As Long
Dim lBufferSize As Long
Dim sBuffer As String
Dim bBuffer() As Byte
lLen = UBound(bUTF8) + 1
If lLen = 0 Then Exit Function
lBufferSize = lLen * 2
sBuffer = String$(lBufferSize, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bUTF8(0)), lLen, StrPtr(sBuffer), lBufferSize)
If lRet <> 0 Then
sBuffer = Left(sBuffer, lRet)
End If
UTF8_Decode = sBuffer
End Function