欢迎光临:
  
  非常感谢您光临枕善居。本站是一个免费的基于VB,VB.NET源代码交流的平台,为大家提供优质的专业的源代码,如果您有需要,本站可以帮助在业余时间里给您寻找代码。当然,如果您有好的代码也可以在本站发布,共享给大家。
专业VB和.NET源码、编程开发教程、图标资源、USB电脑遥控器、智能家电控制开关....更多东东请进入我的淘宝小店--->
VB及.NET新源码2011(3DVD,控件+资源) 智能多路控制(串口编程开关) 带源码!


05-04
26

多功能文本字符串转换函数

一个文本字符串转换函数.
'*参数:
'*strInput 要转换的字符串
'*Method  要转换的格式,对应格式如下
' Method = 0   Text - vbChr
' Method = 1   Text - HTML
' Method = 2   Text - Hex
' Method = 3   vbChr - Text
' Method = 4   HTML - Text
' Method = 5   Hex -Text

Public Function chrConvert(strInput As String, Method As Integer) As String

    If Method = 0 Or Method = 1 Or Method = 2 Then
        chrConvert = ToChr(strInput, Method)
    ElseIf Method = 3 Or Method = 4 Or Method = 5 Then
        chrConvert = ToTxt(strInput, Method)
    End If
End Function


Private Function ToChr(strInput As String, Method As Integer) As String
    strTmp = ""


    For L = 1 To Len(strInput)
        A = Asc(Mid(strInput, L, 1))


        If Method = 0 Then
            strTmp = strTmp & " & Chr(" & A & ")"
        ElseIf Method = 1 Then
            strTmp = strTmp & "&#" & A & ";"
        ElseIf Method = 2 Then
            strTmp = strTmp & "%" & Hex(A)
        End If
    Next L
    If Method = 0 Then strTmp = Right(strTmp, Len(strTmp) - 3)
    ToChr = strTmp
End Function


Private Function ToTxt(strInput As String, Method As Integer) As String
    Dim F As Integer, H As Integer, tmpHex As String, tmpInput As String, inChr() As String
    strTmp = ""


    If Method = 3 Then
        strInput = Replace(strInput, ")", "")
        strInput = Replace(strInput, " & ", "")
        strInput = Replace(strInput, "Chr(", "")
    ElseIf Method = 4 Then
        strInput = Replace(strInput, "&#", "")
        strInput = Replace(strInput, ";", "")
    ElseIf Method = 5 Then
        strInput = Replace(strInput, "%", "")
        strInput = Replace(strInput, " ", "")


        For H = 1 To Len(Text1) Step 2
            tmpHex = tmpHex & "" & Mid(strInput, H, 2)
        Next H
        strInput = tmpHex
    End If
    inChr = Split(strInput, "")


    For F = 1 To ChrNum(strInput)
        inChr(F) = AddZeros(3 - Len(inChr(F))) & inChr(F)
        tmpInput = tmpInput & inChr(F)
    Next F


    For L = 1 To Len(tmpInput) Step 3
        A = Mid(tmpInput, L, 3)
        If Method = 5 Then A = Val("&H" & A)
        strTmp = strTmp & Chr(A)
    Next L
    ToTxt = strTmp
End Function


Private Function ChrNum(strInput As String) As Integer
    Dim lngLen As Long, lngFound As Long, lngEnd As Long
    ChrNum = 0
    lngLen& = Len(strInput$)
    lngFound& = InStr(strInput$, "")


    Do While lngFound& <> 0
        lngFound& = InStr(lngFound& + 1, strInput$, "")
        ChrNum = ChrNum + 1
    Loop
End Function


Private Function AddZeros(Number As Integer) As String
    Dim Z As Integer, tmpZeros As String


    For Z = 1 To Number
        tmpZeros = tmpZeros & "0"
    Next Z
    AddZeros = tmpZeros
End Function


相关日志:
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
评论: 2 | 引用: 0 | 查看次数: 5977

回复回复ahuatian2008 [2008-01-27 16:37:08 |  | del]
谢谢分享!!
回复回复woljgood [2006-02-27 20:25:20 |  | del]
不能为中文啊!
发表评论
您没有权限发表评论!