VB utf8转unicode的代码!要汉字不会乱码的!

急求一个utf8转unicode的代码!要转换后汉字不会乱码、问号、漏字的。要能完美转换汉字!在网上都快翻遍了也没找到好的,都是会乱码、漏字、出问号。急求!如果能完美解决... 急求一个utf8转unicode的代码!要转换后汉字不会乱码、问号、漏字的。要能完美转换汉字!在网上都快翻遍了也没找到好的,都是会乱码、漏字、出问号。急求!如果能完美解决再加分! 展开
 我来答
棘棘芽
推荐于2016-07-13 · TA获得超过563个赞
知道小有建树答主
回答量:608
采纳率:100%
帮助的人:558万
展开全部
试下这个代码 如果你的UTF8 没有错误编码 应该不会错
Private BasicChr() As Byte = {&H80, &H40, &H20, &H10, &H8, &H4}
Function UTFToUnicode(bytes() As Byte) As String
On Error Resume Next
Dim l As long,i as long,j as long
l = UBound(bytes)
Dim s As String
s = ""
For i = 0 To l
Dim index As long
Dim b As long
For index = 0 To UBound(BasicChr)
If (bytes(i) And BasicChr(index)) = 0 Then Exit For
Next
b = bytes(i)
If index > 1 Then
For j = 1 To index - 1
If (bytes(i + j) And &HC0) = &H80 Then
' b = (b << 6) Or (bytes(i + j) And &H3F)
b = (b * &H40) Or (bytes(i + j) And &H3F)
Else
MsgBox("不是UTF编码 :&H" & Hex(bytes(i + j)))
End If
Next
i += index - 1
End If
Select Case index
Case 0
b = b And &H7F
Case 1 '
Case 2
b = b And &H7FF
Case 3
b = b And &HFFFF
Case 4
b = b And &H1FFFFF
Case 5
b = b And &H3FFFFFF
Case 6
b = b And &H7FFFFFFF
End Select
s = s & ChrW(b)
's &= ChrW(b)
Next
UTFToUnicode = s
'Return s
End Function
更多追问追答
追问
第一句就是错的
还有这句 i += index - 1 这句是VB.NET代码吧
追答
嗯 现在不经常用vb了 都vb.net 做的    下面的就可以了 已经修改 试验 通过
Function UTFToUnicode(bytes() As Byte) As String
Static BasicChr(5) As Byte, islond As Boolean
If Not islond Then
BasicChr(0) = &H80
BasicChr(1) = &H40
BasicChr(2) = &H20
BasicChr(3) = &H10
BasicChr(4) = &H8
BasicChr(5) = &H4
islond = True
End If
On Error Resume Next
Dim l As Long, i As Long
l = UBound(bytes)
Dim s As String
s = ""
For i = 0 To l
Dim index As Long
Dim b As Long, j As Long
For index = 0 To UBound(BasicChr)
If (bytes(i) And BasicChr(index)) = 0 Then Exit For
Next
b = bytes(i)
If index > 1 Then
For j = 1 To index - 1
If (bytes(i + j) And &HC0) = &H80 Then
b = (b * &H40) Or (bytes(i + j) And &H3F)
Else
MsgBox ("不是UTF编码 :&H" & Hex(bytes(i + j)))
End If
Next
i = i + index - 1
End If
Select Case index
Case 0: b = b And &H7F&
Case 1 '
Case 2: b = (b And &H7FF&)
Case 3: b = (b And &HFFFF&)
Case 4: b = (b And &H1FFFFF)
Case 5: b = (b And &H3FFFFFF)
Case 6: b = (b And &H7FFFFFFF)
End Select
s = s & ChrW(b)
Next
UTFToUnicode = s
End Function
百度网友ba88284
2013-01-16 · TA获得超过1521个赞
知道大有可为答主
回答量:1822
采纳率:73%
帮助的人:577万
展开全部
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim utfLen As Long

utfLen = -1
On Error Resume Next
utfLen = UBound(Utf)
If utfLen = -1 Then Exit Function

On Error GoTo 0

Dim i As Long, j As Long, k As Long, N As Long
Dim B As Byte, cnt As Byte
Dim Buf() As String
ReDim Buf(utfLen)

i = 0
j = 0
Do While i <= utfLen
B = Utf(i)

If (B And &HFC) = &HFC Then
cnt = 6
ElseIf (B And &HF8) = &HF8 Then
cnt = 5
ElseIf (B And &HF0) = &HF0 Then
cnt = 4
ElseIf (B And &HE0) = &HE0 Then
cnt = 3
ElseIf (B And &HC0) = &HC0 Then
cnt = 2
Else
cnt = 1
End If

If i + cnt - 1 > utfLen Then
Buf(j) = "?"
Exit Do
End If

Select Case cnt
Case 2
N = B And &H1F
Case 3
N = B And &HF
Case 4
N = B And &H7
Case 5
N = B And &H3
Case 6
N = B And &H1
Case Else
Buf(j) = Chr(B)
GoTo Continued:
End Select

For k = 1 To cnt - 1
B = Utf(i + k)
N = N * &H40 + (B And &H3F)
Next

Buf(j) = ChrW(N)
Continued:
i = i + cnt
j = j + 1
Loop

Utf8ToUnicode = Join(Buf, "")
End Function
更多追问追答
追问
还是不行,有些字是乱码,例如UTF8的“邀请”,转换成unicode之后就变成“邀诿”
追答
这个已经是很好的啦。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友bdb9803
2013-01-16 · TA获得超过1.1万个赞
知道大有可为答主
回答量:1.1万
采纳率:53%
帮助的人:1亿
展开全部
编码是什么?能贴出来吗?最好是字节数组,我看看转换后的结果
更多追问追答
追问
UTF-8编码,VB通过winsock get到的
追答
你能贴出来一部分吗?类似于A8 06 C9 AD.....这样的16进制码,我有一些转换的代码,看能不能百分之百正确转换
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式