Share via

Setting the Code Page for String Conversions

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

ms525789.alert_caution(en-us,VS.90).gifImportant Note:

Active Server Pages, and the script engines it supports, internally use Unicode, a 16-bit fixed-width character encoding standard. ASP, however, does not support Unicode Web pages or strings.

Character Encoding on the IIS Server

If you create a Web page on a computer whose default code page is the same as the default code page of the Web server, you do not have to declare a code page in your Web page, though it is recommended.

If you create a Web page on a computer whose default code page is not the same as the default code page of the Web server, you need to specify the code page so that strings are correctly converted internally to unicode and back again.

To specify the code page 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 code page to Japanese, set the AspCodePage property for your application to 932 using one of the following commands.

IIS 5.0 and earlier: Response.CodePage is not available.

<%@ CODEPAGE = 932 %> 
<% Session.CodePage = 932 %> 
<% Response.CodePage = 932 %> 

If you use the @CodePage directive, you must save the ASP page 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.

When a script is executed, IIS determines how characters are encoded using the following hierarchy:

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

Character Encoding on the Client's Browser

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. 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 code page back to its original value. The following script shows one way to temporarily change the code page:

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