VB.net(VB6)String类型、中文、日文与Unicode互相转换

VB.net(VB6)String类型、中文、日文与Unicode互相转换

***需要引用正则表达式

Imports System.Text.RegularExpressions

①将unicode转成中文,如:ABC \u8033\u9EA6 12345,转后为:ABC 耳麦 12345

 '将unicode转成中文,如:ZRO \u8033\u9EA6 12345,转后为:ZRO 耳麦 12345
    Public Function UnicodeToString(strCode As String) As String
        UnicodeToString= strCode
        If InStr(UnicodeToString, "\u") <= 0 Then
            Exit Function
        End If
        strCode = LCase(strCode)
        Dim mc As MatchCollection
        mc = Regex.Matches(strCode, "\\u\S{1,4}")
        For Each m In mc
            strCode = Replace(strCode, m.ToString, ChrW("&H" & Mid(CStr(m.ToString), 3, 6)))
        Next
        UnicodeToString= strCode
    End Function

②将中文转为unicode编码,如:ABC 耳麦 12345,转后为:ABC \u8033\u9EA6 12345

'将中文转为unicode编码,如:ZRO 耳麦 12345,转后为:ZRO \u8033\u9EA6 12345
Function StringToUnicode(strCode As String) As String
    Dim a() As String
    Dim str As String
    Dim i As Integer
    For i = 0 To Len(strCode) - 1
        On Error Resume Next
        str = Mid(strCode, i + 1, 1)
        If isChinese(str) = True Then '//是中文
            StringToUnicode= StringToUnicode & "\u" & StrDup(4 - Len(Hex(AscW(str))), "0") & Hex(AscW(str))
        Else '//不是中文
            StringToUnicode= StringToUnicode & str
        End If
    Next
End Function

'是否为中文
Public Function isChinese(Text As String) As Boolean

    Dim l As Long
    Dim i As Long
    l = Len(Text)
    isChinese = False

    For i = 1 To l
        If Asc(Mid(Text, i, 1)) < 0 Or Asc(Mid(Text, i, 1)) < 0 Then
            isChinese = True
            Exit Function
        End If
    Next

End Function

参考文献:
【1】https://blog.csdn.net/boys1999/article/details/23214065

你可能感兴趣的:(VB.net)