Setting the Code-Page for String Conversions

A codepage is an internal table that the operating system uses to map symbols (letters, numerals, and punctuation characters) to a number. Different codepages provide support for the character sets used in different languages. Code pages are referred to by number; for example, codepage 932 represents the Japanese character set, and codepage 950 represents one of the Chinese character sets.

Active Server Pages, and the script engines it supports, internally use Unicode, a 16-bit fixed-width character encoding standard. If you author all of your pages in the default codepage of the Web server, ASP automatically converts strings. If your script was not created for the Web server's default codepage or the browser's default codepage, you need to specify the code-page so that strings are correctly converted as they are passed during the following scenarios:

To specify the codepage for an ASP page, use the AspCodePage metabase property, the @CODEPAGE directive, the Session.CodePage property, or the Response.CodePage property. For example, to set the codepage to Japanese, set the AspCodePage property for your application to 932 using one of the following commands:

<%@ CODEPAGE = 932 %>

<% Session.CodePage = 932 %>

<% Response.CodePage = 932 %>

Keep in mind that the ASP page must be saved in the file format that matches the @CodePage directive. For example, if @CodePage is set to 65001, the Web page must be saved in UTF-8 format.  If @CodePage is set to 932, the Web page must be saved in ANSI format on a computer that has the default system locale set to Japanese.

As ASP processes the content and script on this page, it uses the code page you have specified to convert characters from your script's character set into Unicode. For example, the value that refers to the letter "a" in ANSI is converted into the different value that refers to the letter "a" in Unicode.



When a script is executed, Response.CodePage determines how characters are encoded. If Response.CodePage is not set explicitly in a Web page, then it is set implicitly with this hierarchy:

There can only be one codepage per Web page. If you use @CodePage and one other method, make sure you set them to the same codepage, or one of them to UTF-8. @CodePage affects literal strings and the other methods affect string variables, so if two incompatible codepages are mixed, the browser displays the literal strings incorrectly.

When ASP sends a Web page to a browser, the browser needs to know what character set to use to display the page properly, and the language pack for that character set must be installed on the computer. To specify the character set, use one of the following commands:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=windows-1251">
<% Response.Charset = "windows-1252"

If you are temporarily changing the code page for a portion of your script, be sure to set the codepage back to its original value. The following script shows one way to temporarily change the codepage:

<%@ CodePage = 65001 %>
<!-- Welcome to my home page in UTF-8 -->
<%
  Response.Write "The Session codepage is " & Session.CodePage & "<BR>"
  Response.CodePage = 932
  Response.Write "The codepage for this page has been changed with Response.CodePage<BR>"
  Response.Write "The Response codepage is " & Response.CodePage & "<BR>"
  Response.Write "The Session codepage is still " & Session.CodePage & "<BR>"
%>

For more examples and information, please see the documentation for AspCodePage, @CodePage, Session.CodePage, Response.CodePage, and Response.Charset.


© 1997-2001 Microsoft Corporation. All rights reserved.