How to URLEncode in UTF8 with Visual Basic 6

How to URLEncode in UTF8 with Visual Basic 6

Visual Basic 6 (VB6) is like a mythical creature: no matter how hard you try to get rid of it, it always comes back haunting you sooner or later: most of the times it assumes the form of some old and outdated home-made software you really would like to dismiss, yet you still have it somewhere. Today we had another resurrection in our office involving a very old client-based software that we still use here and there.

The issue we had to face was related to the following URLEncode function hidden within the source code:

Which worked fine if you can live with Windows-1252 strings, yet is not ideal when you work with UTF-8 only. For example, if you have the À character, the following function will convert it to %C0, which is hardly what you want: if you plan to URLDecode that string in a UTF-8 environment, you’ll end up having an invalid character rather than the original one.

In order to fix that we quickly arranged the following static class which fixed our problem. This is basically an UTF16-into-UTF8 URLEncode that happens to be compatible with most URLDecode built-in functions (PHP, ASP.NET, JAVA and so on).

If you stumbled upon this post, such class should fix your problem too.



About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies.

View all posts by Ryan