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 character number. Different code pages provide support for the character sets used in different countries. 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.

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 code page of the Web server, ASP automatically converts strings. If your script was not created in the Web server's default code page, however, you need to specify the code page so that strings are correctly converted as they are passed:

To specify the code page for an ASP page, use the @ CODEPAGE directive. For example, to set the code page to Japanese, use the following directive:

<%@ CODEPAGE = 932 %>
 

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 will be converted into the different value that refers to the letter "a" in Unicode.

Active Server Pages assumes that strings passed between the Web server and the browser, or between your script and COM components, are in the same code page you have set for your script. If you need to specify a different code page, you can set the Session.CodePage property to override the CODEPAGE setting. For example, you may have authored your script in JIS but need to respond to a client that is using UTF-8 (two different character encodings for the standard Japanese character set).

Session.CodePage defaults to the value of the @ CODEPAGE directive; setting it overrides the current CODEPAGE setting. For example, to change the code page to one of the Chinese character sets, use the following command:

<% Session.CodePage = 950 %>
 

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

<!-- Welcome to my home page in Japanese, code page 932 --!>
<%
  @CodePage = 932 
  Session("OriginalCodePage") = Session.CodePage 
  <!-- Look up name in Chinese, code page 950 --!>
  Session.CodePage = 950 
  Sender = ReadMailHeader("Sender") 
  Found = FindFriend("Sender") 
<!-- Restore the original code page --!>
  Session.CodePage =  Session("OriginalCodePage")
  If Found == TRUE 
    ReplyWithPersonalizedForm()
  Else
    ReplyWithBusinessForm()
%>