最近用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 <> "<" And t1 <> ">" And t1 <> "&apos" And t1 <> """ And t1 <> "&" And t1 <> " " Then
ToDoString = Left(ToDoString, p2 - 1) + Replace(ToDoString, "&", "&", p2, 1)
End If
Else
ToDoString = Left(ToDoString, p2 - 1) + Replace(ToDoString, "&", "&", 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
'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 <> "<" And t1 <> ">" And t1 <> "&apos" And t1 <> """ And t1 <> "&" And t1 <> " " Then
ToDoString = Left(ToDoString, p2 - 1) + Replace(ToDoString, "&", "&", p2, 1)
End If
Else
ToDoString = Left(ToDoString, p2 - 1) + Replace(ToDoString, "&", "&", 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