Classic ASP: How to write unicode string data in classic ASP?

RedsDevils picture RedsDevils · Dec 6, 2009 · Viewed 25.7k times · Source

How can I show an nvarchar column that stores unicode data (Entered with the zawgyi1 font) in a classic ASP web page?

When I retrieve and write the value to the page, it shows "?????". I set my ASP page's content type of UTF-8 with the following meta tag:

<META http-equiv="Content-Type" content="text/html; charset=UTF-8">

Unfortunately, the text is still rendered as "?????".

Any suggestions or ideas on how to display unicode values in a classic ASP page?

Answer

AnthonyWJones picture AnthonyWJones · Dec 6, 2009

The Content-Type meta header informs the browser to treat the content sent as a UTF-8 encoded text stream. It doesn't ensure that the stream sent is actually UTF-8. To handle UTF-8 correctly you need to do 3 things:-

  1. Ensure your static content is saved in a UTF-8 compatible encoding.
  2. Ensure your dynamic content is encoded to UTF-8.
  3. Inform the client that the content is UTF-8 encoded.

Item 1 requires either that you actually save the ASP file as a UTF-8 encoded file or that all your static content in the file is within the ASCII character range (0-127). Note if you save as UTF-8 then all your server-side script must use characters within the ASCII character range. In Visual Studio you can do so by "Saving the file AS..." and then clicking on the little arrow on the Save button.

Item 2 requires that the Response.CodePage property is set to the UTF-8 code page 65001, you can do this in code or by adding the attribute CODEPAGE=65001 to the <%@ %> declarations on the first line of the ASP file. If you do it in code you must set it before any calls to Response.Write. AND: do not use chr or asc functions (these are buggy when using 65001) but use chrw and ascw instead.

Item 3 requires that the Content-Type header contains the charset=UTF-8 qualifier. As you are already doing you can do this with the META header. Personally I find that to be a bit of kludge, I prefer to use Response.Charset = "UTF-8" in code. This places the qualifier on the true Content-Type HTTP header.