VB GB2312 转 UTF-8 函数

2011-04-20  赵黄健 

最近用VB写一个小东西用socket跟别的软件进行数据通讯,一直说我连接了但是没有数据......
后来看了下要转成UTF-8编码格式.
百度V5
'*******************************************************************
'Gb2312转为UTF-8的函数。
Public Function Ascii(ByVal ToDoString)
        On Error Resume Next
'        If LCase(ChineseType) = "complex" Then ToDoString = JToF(ToDoString)
'        If LCase(ChineseType) = "simple" Then ToDoString = FToJ(ToDoString)
            Dim strOutBuf
            Dim p1, p2, p3
            Dim t1, t2
            p1 = 1
            p2 = InStr(p1, ToDoString, "&")
            While p2 > 0
                p3 = InStr(p2, ToDoString, ";")
                If p3 > 0 Then
                    t1 = Mid(ToDoString, p2, p3 - p2)
                    If LCase(Left(t1, 3)) = "&#x" And IsNumeric("&H" + Mid(t1, 4)) Then
                    ElseIf Left(t1, 2) = "&#" And IsNumeric(Mid(t1, 3)) Then
                    ElseIf t1 <> "&lt" And t1 <> "&gt" And t1 <> "&apos" And t1 <> "&quot" And t1 <> "&amp" And t1 <> "&nbsp" Then
                        ToDoString = Left(ToDoString, p2 - 1) + Replace(ToDoString, "&", "&amp;", p2, 1)
                    End If
                Else
                    ToDoString = Left(ToDoString, p2 - 1) + Replace(ToDoString, "&", "&amp;", p2, 1)
                End If
                p2 = InStr(p2 + 1, ToDoString, "&")
            Wend
            For t1 = 1 To Len(ToDoString)
                If AscW(Mid(ToDoString, t1, 1)) > 255 Or AscW(Mid(ToDoString, t1, 1)) < 32 Then
                    If AscW(Mid(ToDoString, t1, 1)) <> 0 Then strOutBuf = strOutBuf + "&#x" + Hex(AscW(Mid(ToDoString, t1, 1))) + ";"
                Else
                    strOutBuf = strOutBuf + Mid(ToDoString, t1, 1)
                End If
            Next
            Ascii = strOutBuf
End Function
378°/3771 人阅读/1 条评论 发表评论

张林  2011-09-11

什么系统要到这种转换?


登录 后发表评论
赵黄健
访客 3406
赵黄健 的其他博文 更多