VB怎样把UTF—8转换成GB2312
请高手们帮帮忙,代码要详细点。我要转换的UTF—8在TEXT1文本中。怎么看不到你们的回答!?...
请高手们帮帮忙,代码要详细点。我要转换的UTF—8在TEXT1文本中。
怎么看不到你们的回答!? 展开
怎么看不到你们的回答!? 展开
2个回答
推荐于2016-08-22
展开全部
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
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim lRet As Long
Dim lLength As Long
Dim lBufferSize As Long
lLength = UBound(Utf) - LBound(Utf) + 1
If lLength <= 0 Then Exit Function
lBufferSize = lLength * 2
Utf8ToUnicode = String$(lBufferSize, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf(0)), lLength, StrPtr(Utf8ToUnicode), lBufferSize)
If lRet <> 0 Then
Utf8ToUnicode = Left(Utf8ToUnicode, lRet)
Else
Utf8ToUnicode = ""
End If
End Function
以上代码放在一个模块中
以下代码是放在窗体中,添加一个命令按钮Command1
一个文本框Text1 是存放要转换的UTF-8文本
一个文本框Text2 是存放转换后的GB2312文本
Private Sub Command1_Click()
Dim b() As Byte
b = StrConv(Text1.Text, vbFromUnicode)
Text2.Text = Utf8ToUnicode(b)
End Sub
Private Const CP_UTF8 = 65001
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim lRet As Long
Dim lLength As Long
Dim lBufferSize As Long
lLength = UBound(Utf) - LBound(Utf) + 1
If lLength <= 0 Then Exit Function
lBufferSize = lLength * 2
Utf8ToUnicode = String$(lBufferSize, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf(0)), lLength, StrPtr(Utf8ToUnicode), lBufferSize)
If lRet <> 0 Then
Utf8ToUnicode = Left(Utf8ToUnicode, lRet)
Else
Utf8ToUnicode = ""
End If
End Function
以上代码放在一个模块中
以下代码是放在窗体中,添加一个命令按钮Command1
一个文本框Text1 是存放要转换的UTF-8文本
一个文本框Text2 是存放转换后的GB2312文本
Private Sub Command1_Click()
Dim b() As Byte
b = StrConv(Text1.Text, vbFromUnicode)
Text2.Text = Utf8ToUnicode(b)
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询