为什么ListView控件显示从MySQL数据库中读出来的汉字是乱码????急!!!

ziwanli 2006-04-10 04:00:43
我用vb远程连接MySQL数据库,连接成功后,用ListView 控件显示时,凡是有中文的地方就是乱码,请问该如何解决呢????请高手指点!!!
...全文
221 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
僵哥 2006-04-11
Private Function Utf8_Decode(ByVal UTF8_String As String) As String
Dim Utf8_Binary() As Byte '//UTF8 Binary Source
Dim Utf16_Binary() As Byte '//Unicode Binary
Dim Utf8_BytesLength As Long '//UTF8 String Length in Binary
Dim Utf16_Length As Long '//Unicode Binary Length with Terminal Symbol &H0000
Dim Binary_Byte As Byte '//UTF8 Byte
Dim WideChar_Long As Long '//Unicode Char
Dim Source_Counter As Long '
Dim Dest_Counter As Long

Utf8_Decode = "" '//Predefine a return value
If UTF8_String = "" Then Exit Function

Utf8_Binary = UTF8_String '//Copy to Binary Array
Utf8_BytesLength = UBound(Utf8_Binary) + 1
Utf16_Length = (Utf8_BytesLength + 1) * 2
ReDim Utf16_Binary(Utf16_Length) '//Get Memory for Unicode String

Source_Counter = 0
Dest_Counter = 0

While (Source_Counter < Utf8_BytesLength) And (Dest_Counter < (Utf16_Length \ 2))
WideChar_Long = Utf8_Binary(Source_Counter)
Source_Counter = Source_Counter + 1

If (WideChar_Long And &H80&) <> 0 Then
If Source_Counter >= Utf8_BytesLength Then Exit Function '// incomplete multibyte char
WideChar_Long = WideChar_Long And &H3F&
If (WideChar_Long And &H20&) <> 0 Then
Binary_Byte = Utf8_Binary(Source_Counter)
Source_Counter = Source_Counter + 1
If (Binary_Byte And &HC0&) <> &H80& Then Exit Function '// malformed trail byte or out of range char
If Source_Counter >= Utf8_BytesLength Then Exit Function '// incomplete multibyte char
WideChar_Long = (WideChar_Long * (2 ^ 6)) Or (Binary_Byte And &H3F&)
End If
Binary_Byte = Utf8_Binary(Source_Counter)
Source_Counter = Source_Counter + 1
If (Binary_Byte And &HC0&) <> &H80& Then Exit Function '// malformed trail byte
Utf16_Binary(Dest_Counter * 2 + 1) = (((WideChar_Long * (2 ^ 6)) Or (Binary_Byte And &H3F&)) And &HFF00&) \ (2 ^ 8)
Utf16_Binary(Dest_Counter * 2) = ((WideChar_Long * (2 ^ 6)) Or (Binary_Byte And &H3F&)) And &HFF&
Else
Utf16_Binary(Dest_Counter * 2 + 1) = (WideChar_Long And &HFF00&) \ (2 ^ 8)
Utf16_Binary(Dest_Counter * 2) = WideChar_Long And &HFF&
End If
Dest_Counter = Dest_Counter + 1
Wend

If Dest_Counter >= Utf16_Length \ 2 Then Dest_Counter = Utf16_Length \ 2 - 1

Utf16_Binary(Dest_Counter * 2) = 0 '//Terminal Symbol
Utf16_Binary(Dest_Counter * 2 + 1) = 0 '//Terminal Symbol

ReDim Preserve Utf16_Binary(Dest_Counter * 2 + 1) '//effective bytes only

Utf8_Decode = Utf16_Binary '//Convert binary array to unicode string
End Function
回复
ziwanli 2006-04-10
好的,谢谢僵哥!!
回复
僵哥 2006-04-10
需要聲明.
Private Declare Function MultiByteToWideChar _
Lib "kernel32" (ByVal CodePage As Long, _
ByVal dwFlags As Long, _
ByVal lpMultiByteStr As String, _
ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As String, _
ByVal cchWideChar As Long) _
As Long

Private Const CP_UTF8 = 65001 'CODE PAGE FOR UTF8
回复
ziwanli 2006-04-10
我使用了MultiByteToWideChar这个函数,可是编译器说我没有定义这个函数,什么原因??
回复
僵哥 2006-04-10
Private Declare Function MultiByteToWideChar _
Lib "kernel32" (ByVal CodePage As Long, _
ByVal dwFlags As Long, _
ByVal lpMultiByteStr As String, _
ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As String, _
ByVal cchWideChar As Long) _
As Long

Private Const CP_UTF8 = 65001 'CODE PAGE FOR UTF8


Public Function Decode_UTF8(ByVal sText As String)

Dim sBuffer As String
Dim nLength As Long
Dim byText() As Byte

'''' CONVERT UTF8 TO UNICODE '''''''''''''''''''''''''''''''''''''''''''''''
If LenB(sText) = 0 Then
Decode_UTF8 = ""
Exit Function
End If 'IF NOTHING TO CONVERT, DON'T BOTHER

byText() = StrConv(sText, vbFromUnicode)
nLength = UBound(byText()) + 1
sBuffer = String(nLength * 2, vbNullChar)

nLength = MultiByteToWideChar(CP_UTF8, 0, byText(0), nLength, _
StrPtr(sBuffer), nLength * 2)
'CONVERT TO UNICODE AND STORE IN sBuffer

If nLength > 0 Then sText = Left(sBuffer, nLength)
'TRIM IF REQUIRED
Decode_UTF8 = sText
End Function


據說上面這是一個轉換函數,但是我現在無法進行測試.
回复
ziwanli 2006-04-10
哦,我试试!!谢!!
回复
僵哥 2006-04-10
對於Win2k以上系統可以使用MultiByteToWideChar進行轉換,但是對於Win9X系統不行.如果需要倒是可以給一個動態鏈接庫(用C++/Delphi寫的).
回复
ziwanli 2006-04-10
该怎么转换啊,能请教一下吗?
回复
僵哥 2006-04-10
MySQL使用的是UTF8編碼,所以需要轉換.
回复
相关推荐
发帖
控件
创建于2007-09-28

1429

社区成员

VB 控件
申请成为版主
帖子事件
创建了帖子
2006-04-10 04:00
社区公告
暂无公告