Re: how to decode URI Component in vb6(removing %% from urls)?
I was playing with this
Code:
Option Explicit
Private Function ConvertToString(ByVal strD As String) As String
Dim strN As String
Dim intPos As Integer
Do
intPos = InStr(strD, "%")
If intPos > 0 Then
strN = Chr(Val("&H" & Mid$(strD, intPos + 1, 2)))
Mid$(strD, intPos, 1) = strN
strD = Mid$(strD, 1, intPos) & Mid$(strD, intPos + 3, Len(strD) - (intPos + 1))
End If
Loop Until intPos <= 0
ConvertToString = strD
End Function
Private Sub Command_Click()
Dim strD As String
strD = "http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab"
Debug.Print ConvertToString(strD)
End Sub
but couldn't see how to convert %C3%A5 into å (hex E3)
Re: how to decode URI Component in vb6(removing %% from urls)?
Isn't there a reverse to URLEncode?
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Re: how to decode URI Component in vb6(removing %% from urls)?
Originally Posted by Doogle
but couldn't see how to convert %C3%A5 into å (hex E3)
mainly because http://w3schools.com/my test.asp?name=ståle&car=saab actually encodes to http%3A%2F%2Fw3schools%2Ecom%2Fmy+test%2Easp%3Fname%3Dst%E5le%26car%3Dsaab
Re: how to decode URI Component in vb6(removing %% from urls)?
What you have there is a UTF-8 URL.
These are common now, because the standard long ago changed from a mix of ASCII and whatever else people felt like doing. The mishmash that resulted plus the desire for full Unicode support led to the change. Remember, the Internet was never developed for general international use when first constructed, so year by year we get more patches on top of patches. Browsers are a mess inside, for this and many other reasons.
There are some nice API calls in SHLWAPI.DLL for URL encoding and decoding. But until Windows 8 they do not handle UTF-8. They were built for the safe, 7-bit ASCII subset of ANSI and for UTF-16LE/UCS-2 (which VBers tend to think of as "Unicode" even though Unicode means many things).
With a hack and a prayer you can combine the SHLWAPI calls with conversion API calls, and almost get it right most of the time. This isn't safe though (it fails on some characters), so you're back to some hand-rolled logic.
The attached demo shows one way to reliably accomplish this. Of course that doesn't mean there aren't bugs there either since I threw it together quickly. Test it to find and fix lingering errors.
Most (if not all?) of the links posted above were obsolete the day they were posted, so you can safely ignore them. People should really spend a few minutes to read 2003's famous exposition:
Re: how to decode URI Component in vb6(removing %% from urls)?
Hi dilettante,
Thank you for posting your very nice code. I tried it and it works great.
However I am trying to solve the OPPOSITE problem. I need to encode the text from non-English language to format that Bing.com can understand. Let's take the word "фиттнесс" (means "fittness" in Russian). All encoders I found so far converts it into %F4%E8%F2%F2%ED%E5%F1%F1 . And it is wrong line because call to Bing like http://www.bing.com/search?q=%F4%E8%F2%F2%ED%E5%F1%F1
returns NOTHING.
The correct line for the word "фиттнесс" is %D1%84%D0%B8%D1%82%D1%82%D0%BD%D0%B5%D1%81%D1%81 .
I tried to decode this word with your decoder and it worked great. However how I can encode this "фиттнесс" to "%D1%84%D0%B8%D1%82%D1%82%D0%BD%D0%B5%D1%81%D1%81" using VB6???
Re: how to decode URI Component in vb6(removing %% from urls)?
Or, you could get the source code from that Web Site and use the Java Script in it. It's OK because they say you can copy the source code and save it to your hard drive.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to decode URI Component in vb6(removing %% from urls)?
Originally Posted by jmsrickland
Or, you could get the source code from that Web Site and use the Java Script in it. It's OK because they say you can copy the source code and save it to your hard drive.
Actually I took a look and they oversimplify the process by a ton (producing incorrect results), but then again I did much the same in my demo above as well. The problem is the %-ecoding of things like the "://" after after "http" in a URL.
Here is an attempt to do it "properly"... well almost properly, see the comments in my UrlQueryStringEncode() function. But it should be good enough for what you need I hope. Again, see the comments if you need to perfect the process.
There are a ton of rules for fiddling with URLs. The parts before a query have one set of rules and whatever comes as the querystring of a URL have whatever rules the target web site chose to use more or less.
For this reason there aren't any simple API or object method calls that do entirely what you are asking for. So in this program I'm grabbing off the query part, encoding that (using a hack for pre-Win7 systems), and then glueing the two parts back together.
You could also just rip out quite a few of the API calls and just use my "pre-Win7 hack" (i.e. Utf8Escape() function) by itself if you pass it a querystring that you want to convert to escaped UTF-8.
Warning:
In order to allow the use of Unicode text, I am using the Microsoft InkEdit Control instead of a TextBox. This is already part of Windows Vista and later though, and there should still be an inkless version available for installation into WinXP and Win2K. Or you might substitute an MS Forms 2.0 TextBox here.
Last edited by dilettante; Dec 11th, 2013 at 11:49 PM.
Reason: Fixed ridiculous bug!
Re: how to decode URI Component in vb6(removing %% from urls)?
Hi dilettante,
Thank you VERY MUCH for your excellent code. You actually solved more complex problem with extraction of query from URL search string. What I was needed - only convertsion of non-English words to esc format acceptable by Bing.com. Anyhow, your code works great and gets the job done. Is it OK for you if I will use your posted code in my application (free of charge )?
Re: how to decode URI Component in vb6(removing %% from urls)?
Glad it is helpful.
Sure, go ahead and use it. I made no license claims in the source so the only encumbrance is the implied Copyright. My understanding is that the way it was published here allows you to do anything you want with the code except (a.) claim that you are the original author, or (b.) anything that violates VBForums' Content Licensing terms.
Re: how to decode URI Component in vb6(removing %% from urls)?
@dilettante
From your post #10 are you stating your app will not function on XP?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Re: how to decode URI Component in vb6(removing %% from urls)?
To run it under Windows XP you would have to have the Microsoft Ink controls installed (which do not ship in XP except for Windows XP Tablet PC Edition). You can get these as part of Microsoft Windows XP Tablet PC Edition Software Development Kit 1.7, or it is possible that some 3rd party application may have installed the InkEdit control on your system already.
Or you could replace the upper "textbox" in the picture with an MS Forms 2.0 TextBox or some other Unicode textbox control. Or if you were running with Russian language locale settings a plain old ANSI VB6 intrinsic TextBox might be (?) good enough.
In this case it is all about being able to enter the foreign language text (Russian here).
Last edited by dilettante; Dec 12th, 2013 at 02:07 PM.
Re: how to decode URI Component in vb6(removing %% from urls)?
Funny that on the first link they show you a count down timer for the end of support on XP and then over on the right they tell you to download SP3 and 4 Steps to Speed Up XP
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.