how to decode URI Component in vb6(removing %% from urls)?
Hi all . I got urls as variable but they are in diffrent format like this:
http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab
I wonder how i can remove those % and make it like this?:
http://w3schools.com/my test.asp?name=ståle&car=saab
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?
Re: how to decode URI Component in vb6(removing %% from urls)?
Re: how to decode URI Component in vb6(removing %% from urls)?
Quote:
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
2 Attachment(s)
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:
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
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???
There is online encoder on http://meyerweb.com/eric/tools/dencoder/ , but I need to repliate this functionality in VB6.
I will greatly appreciate your help.
Re: how to decode URI Component in vb6(removing %% from urls)?
You could probably just reverse what the program above does, calling WideCharToMultiByte to go the opposite direction.
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.
2 Attachment(s)
Re: how to decode URI Component in vb6(removing %% from urls)?
Quote:
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.
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?
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).
Re: how to decode URI Component in vb6(removing %% from urls)?
I just tested a clean XP SP 3 system, and sure enough:
Quote:
Component 'Inked.dll' or one of its dependencies not correctly registered: a file is missing or invalid
Re: how to decode URI Component in vb6(removing %% from urls)?
XP DeathClock
Windows XP End Of Support Countdown Gadget
Quote:
Supported Operating System
Windows 7, Windows Vista
Windows 7 32-bit or 64-bit, any edition.
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
Re: how to decode URI Component in vb6(removing %% from urls)?
Maybe they don't quite believe it.
Re: how to decode URI Component in vb6(removing %% from urls)?
how to decode unicode links like ♥ υ σ?? in vb6