VB 使用C语言Escape的方法

来源:互联网 发布:洋气的英文淘宝店铺名 编辑:程序博客网 时间:2024/06/04 18:06

在C语言中,escape的符号很好用,

比如

 

 "中国一定强"

 

这个字串可以写成:

 

"/x4E2D/x56FD/x4E00/x5B9A/x5F3A"

 

用字元编码编写程序,在其他不同语言的windows运作时,比较不会有问题。

(我尽量不想在程序中写入中文)

但是vb如果全部要用字元编码写的话,就会很麻烦而且一个一个都要手写成:

 

ChrW(&H4E2D) & ChrW(&H56FD) & ChrW(&H4E00) & ChrW(&H5B9A) & ChrW(&H5F3A)

 

所以我写了一条短短的function:

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Public Function EscToStr(ByVal c As String) As String
        Dim tmp, str1, str2, str3, str4 As String
        Dim i, outend1, outend2 As Integer
        i = 1
        outend1 = 1
        Do While outend1 <> 0
            str1 = c
            outend1 = InStr(i, str1, "/x")
            i = outend1 + 1
            outend2 = InStr(i, str1, "/x")

            If outend2 = 0 Then
                outend2 = Len(str1) + 1
            End If
            str2 = Mid(str1, outend1 + 2, outend2 - outend1 - 2)
            str3 = "&H" & str2
            str4 = ChrW(str3)
            tmp = tmp & str4
            If outend2 = (Len(str1) + 1) Then
                outend1 = 0
            End If

        Loop

        EscToStr = tmp
    End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

应用时比如:

Form1.Text=EscToStr("/x4E2D/x56FD/x4E00/x5B9A/x5F3A")

 

显示出的表单文字就成为

 

"中国一定强"