以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  请问这段VB代码转换成狐表怎么转呢?  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=135774)

--  作者:lhpc120
--  发布时间:2019/6/3 4:55:00
--  请问这段VB代码转换成狐表怎么转呢?

\'//将中文转为unicode编码,如:耳麦,转后为:\\u8033\\u9EA6
Function urlUnicodeEncode(strCode As String) As String
    
    Dim a() As String
    Dim str As String
    Dim i As Integer
    StrTemp = strCode
    
    For i = 0 To Len(strCode) - 1
    On Error Resume Next
        str = Mid(strCode, i + 1, 1)
        If isChinese(str) = True Then \'//是中文
            unicodeEncode = unicodeEncode & "\\u" & String(4 - Len(Hex(AscW(str))), "0") & Hex(AscW(str))
        Else \'//不是中文
            unicodeEncode = unicodeEncode & str
        End If
        
    Next
 
End Function
 
\'//是否为中文
Private 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

--  作者:有点甜
--  发布时间:2019/6/3 8:16:00
--  

Public Function StringToUnicode(ByVal value As String) As String
Dim bytes As Byte() = Encoding.Unicode.GetBytes(value)
Dim stringBuilder As StringBuilder = New StringBuilder()

For i As Integer = 0 To bytes.Length - 1 Step 2
    stringBuilder.AppendFormat("\\u{0}{1}", bytes(i + 1).ToString("x").PadLeft(2, "0"c), bytes(i).ToString("x").PadLeft(2, "0"c))
Next

Return stringBuilder.ToString()
End Function


--  作者:lhpc120
--  发布时间:2019/6/5 14:42:00
--  
学习了
--  作者:lhpc120
--  发布时间:2019/6/5 14:49:00
--  
以下是引用有点甜在2019/6/3 8:16:00的发言:

Public Function StringToUnicode(ByVal value As String) As String
Dim bytes As Byte() = Encoding.Unicode.GetBytes(value)
Dim stringBuilder As StringBuilder = New StringBuilder()

For i As Integer = 0 To bytes.Length - 1 Step 2
    stringBuilder.AppendFormat("\\u{0}{1}", bytes(i + 1).ToString("x").PadLeft(2, "0"c), bytes(i).ToString("x").PadLeft(2, "0"c))
Next

Return stringBuilder.ToString()
End Function

请问这段代码应该放在全局代码中吧,如果调用的话怎么调用呢?


--  作者:有点甜
--  发布时间:2019/6/5 15:28:00
--  
以下是引用lhpc120在2019/6/5 14:49:00的发言:

请问这段代码应该放在全局代码中吧,如果调用的话怎么调用呢?

 

放在全局代码里,调用参考

 

Dim s As String = StringToUnicode("耳麦")