如何获得中文字符的utf8格式的编码
我想获得1个中文字符的utf8编码,我使用记事本将文本文件保存为utf8格式的文件,然后用ultraedit来打开它,然后,在16进制下查看它,获得utf8格式的编码,可我发现,这样获得的编码,放到程序中使用的时候,经常在末尾有1个.,请问这是为什么
问题点数:20、回复次数:4Top
1 楼zcsor(偶业余的虽然星星了,但是水平依然是非常业余的。)回复于 2006-07-02 18:01:39 得分 0
。。。。。。。。
好象有简单的方法,好多年没涉及这方面的问题了。
三四年前写过一个SamsungVB30的软件,现在网上应该还能找到,在三星论坛上应该还有,里面发布了代码和程序,你下一个那个程序,里面有大概几万个汉字对应的UTF8
Top
2 楼fxy_2002(阿勇)回复于 2006-07-03 09:34:26 得分 0
'---------------------------------------------------
'UTF8 编码/解码
'--------------------API声明部分--------------------
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar 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
Private Const CP_UTF8 = 65001
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
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 Function
Private 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
Top
3 楼aspower_(敬个礼 握个手 大家都素好朋友!)回复于 2006-07-03 09:47:59 得分 0
不错
有用Top




