如何获得中文字符的utf8格式的编码

wxuedelphi 2006-07-02 05:06:30
我想获得1个中文字符的utf8编码,我使用记事本将文本文件保存为utf8格式的文件,然后用ultraedit来打开它,然后,在16进制下查看它,获得utf8格式的编码,可我发现,这样获得的编码,放到程序中使用的时候,经常在末尾有1个.,请问这是为什么
...全文
207 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
一笑拔剑 2006-07-03
  • 打赏
  • 举报
回复
不错

有用
fxy_2002 2006-07-03
  • 打赏
  • 举报
回复
'---------------------------------------------------
'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
清晨曦月 2006-07-02
  • 打赏
  • 举报
回复
。。。。。。。。
好象有简单的方法,好多年没涉及这方面的问题了。
三四年前写过一个SamsungVB30的软件,现在网上应该还能找到,在三星论坛上应该还有,里面发布了代码和程序,你下一个那个程序,里面有大概几万个汉字对应的UTF8

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧