Special Characters in VBScript

One of the most difficult tasks in VBScript is making special characters viewable. You can easily obtain the ASCII character code for one character, but seeing all the special characters for an entire string is more problematic.

You can write a function to accomplish this task, but VBScript has two built-in functions—Escape and Unescape—that replace special characters to make them URL-safe. VBScript's Help file doesn't document the Escape and Unescape functions. However, if you use an object browser such as Microsoft OLEView to view VBScript's vbscript.dll file, you can see that these functions are implemented.

The Escape function assesses a string argument one character at a time. When the function encounters a special character (i.e., a character other than a letter or number or one of the characters * + - . / @ or _), it replaces the character with a percent sign (%) and the replaced character's hexadecimal number. For example, the line-feed character is ASCII character code 10; 10 in hex notation is 0A—so, Escape replaces a line-feed character embedded in a string with .

Escaping (i.e., replacing) special characters in this manner is common, especially in Internet URLs that don't allow certain characters. For example, the URL for Andrew Clinick's newsgroup posting that documents Microsoft's support for Escape and Unescape (http://groups.google.com/groups?selm=erhw2bjm%24ga.236%40cppssbbsa02.microsoft.com) includes two special characters with Escape codes given: $ (Escape code %24) and @ (Escape code %40). Although VBScript doesn't escape the at (@) character, it does unescape it. For a demonstration, run the following code in a VBScript file:

WScript.Echo Unescape("%40")

You usually can't display characters such as the carriage return and line feed, but Escape lets you at least display their representation. For example, the following code shows the Escape codes for carriage return and line-feed characters:

WScript.Echo Escape(VbCrLf)

Although Escape and Unescape were designed to work with Internet URLs, I use these functions for debugging scripts that use unusual characters. For example, a few months ago I was testing a script that read the output of the Windows shell Ipconfig command. The Escape function showed me that the command inserted some special formatting characters that caused problems with extracting information.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish