Results 1 to 8 of 8

Thread: Can I convert this code easily?

  1. #1
    Guest
    I've got variables in my program filled with file names from HTML file, that end up looking like this:

    "Ed%20Rush%20%26%20Optical"

    I want this to look like:

    "Ed Rush & Optical"

    Is there a function that will convert this for me? Or is there a 'find and replace' function that I could use?

    Thanks for the help!!

    Matt

    ps. If you make a good enough OS (that's still 100% compatible) I know everyone will happily drop windows!!

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Pop this into a module;

    Option Explicit

    Function ConvString(xStr As String) As String
    Dim Dummy As Integer
    ConvString = ""
    For Dummy = 1 To Len(xStr)
    If Mid(xStr, Dummy, 1) = "%" Then
    ConvString = ConvString + Chr(HexToDec(Mid(xStr, Dummy + 1, 2)))
    Dummy = Dummy + 2
    Else
    ConvString = ConvString + Mid(xStr, Dummy, 1)
    End If
    Next
    End Function

    Function HexToDec(xHex As String) As Integer
    HexToDec = 0
    HexToDec = CInt(Left(xHex, 1)) * 16
    HexToDec = HexToDec + CInt(Right(xHex, 1))
    End Function


    then you can use the ConvString function to convert;

    eg;

    MyString = ConvString("Ed%20Rush%20%26%20Optical")

    would return

    Ed Rush & Optical


    It could probably do with a bit of tweaking but it should work (providing all ascii codes that appear after the % signs are two chars long - if they aren't always you will need to fiddle around with the code a bit)

    Have fun.


    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    I've got a find/replace function I have always used.

    Code:
    Public Function String_ReplaceAll(ByVal strSource As String, _
                                      ByVal strWhat As String, _
                                      ByVal strWith As String) As String
    '***************************************************************************
    'Purpose:       Replaces all occurances of one string with another string.
    'Parameters:    strSource - String to be changed.
    '               strWhat   - What needs replacing.
    '               strWith   - What it should be replaced with.
    'Returns:       String    - The changed string.
    '***************************************************************************
    On Error GoTo ErrorHandler
    
    Dim intstrWhatLength As Integer
    Dim intReplaceLength As Integer
    Dim intStart As Integer
    
      intstrWhatLength = Len(strWhat)
      
      If intstrWhatLength = 0 Then
        
        String_ReplaceAll = strSource
        Exit Function
      
      End If
      
      intReplaceLength = Len(strWith)
      intStart = InStr(1, strSource, strWhat)
      
      Do While intStart > 0
          
        strSource = Left(strSource, intStart - 1) + strWith + Right(strSource, Len(strSource) - (intStart + intstrWhatLength - 1))
        intStart = InStr(intStart + intReplaceLength, strSource, strWhat)
      
      Loop
      
      String_ReplaceAll = strSource
    
    End Function
    Hope it helps.

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    You can't use a standard find and replace because you need to scan for the % sign and convert whatever's after it from hex to decimal, then to a chr of that to get the special character - eg %20 = space, %26 = & sign.

    If it was a simple as that you could use the VB6 Replace function.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  5. #5
    Guest
    This should work.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        MsgBox DeHTML("Ed%20Rush%20%26%20Optical")
    End Sub
    
    Private Function DeHTML(ByVal sInput As String)
        Dim lCounter As Long
        For lCounter = 1 To Len(sInput)
            If Mid$(sInput, lCounter, 1) = "%" Then
                DeHTML = DeHTML & Chr("&H" & Mid$(sInput, lCounter + 1, 2))
                lCounter = lCounter + 2
            Else
                DeHTML = DeHTML & Mid$(sInput, lCounter, 1)
            End If
        Next
    End Function

    PS. Forgot to press submit, and when i did other ppl had posted already

    [Edited by Azzmodan on 04-07-2000 at 05:29 PM]

  6. #6
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    I don't think that'll work either 'cos the Chr("&H" +... but will always return & !?

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  7. #7
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Buzby, I was only trying to help, and I misunderstood what was needed, but do try to remember that not everybody has the latest version of VB.

  8. #8
    Guest

    I don't think that'll work either 'cos the Chr("&H" +... but will always return & !?


    Erm i tested it prior to posting in on this forum, and it seemed to work good on my pc..
    Chr("&H" + = Chr("&H" &[/i]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width