![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
ASP Charset Convert Tutorial
Convert Unicode to utf-8, then convert utf-8 to iso-8859-1. <% @CodePage = 65001 %> <% Response.CodePage = 28591 %> <% ' Convert utf-8 (code page 6500) to iso-8859-1 (code page 28591) ' ' IMPORTANT: Remember to save this .asp files in the utf-8 character encoding. ' IMPORTANT: Remember to save this .asp files in the utf-8 character encoding. ' IMPORTANT: Remember to save this .asp files in the utf-8 character encoding. ' The first directive, "@CodePage" is the code page of the ASP file. ' When you save the .asp file using your editor, save it in this code page. ' This is the charset used for literal strings within your ASP scripting. ' ' The Response.CodePage directive indicates the charset encoding emitted ' by Response.Write. ' The charset specified in the <meta> tag, as shown below, must match ' the Response.CodePage. %> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head> <body> <% myStr = "eèéêë" ' myStr is a String. Strings in ASP are Unicode (2 bytes/char). ' This particular .asp file is saved in the utf-8 encoding (our @CodePage is utf-8). ' Therefore, the ASP scripting engine converts the literal string from utf-8 to ' Unicode. (utf-8 is actually the multibyte encoding of Unicode, but it is a different ' character encoding than the 2-byte/char Unicode) ' Create a Chilkat Charset object to convert and examine the character data. set cc = Server.CreateObject("Chilkat_9_5_0.Charset2") cc.UnlockComponent "Anything for 30-day trial" ' Turn on "SaveLast" so we can see what happened... cc.SaveLast = 1 cc.ToCharset = "utf-8" ' If your source data is a String (i.e. not a byte array / Variant) it is Unicode. ' The string type in ASP is *always* Unicode (i.e. ucs-2). utf8Bytes = cc.ConvertFromUnicode(myStr) ' Examine the bytes passed to the converter and the bytes returned: inHexStr = cc.LastInputAsHex outHexStr = cc.LastOutputAsHex Response.Write "<b>Unicode to utf-8 conversion:</b><br>" Response.Write "Input: " + inHexStr + "<br>" Response.Write "Output: " + outHexStr + "<p>" ' This is the output: ' Input: 6500 E800 E900 EA00 EB00 ' Output: 65C3 A8C3 A9C3 AAC3 AB ' We can clearly see that the input was Unicode (2 bytes/char). ' The output is utf-8. The individual characters are: 65 C3A8 C3A9 C3AA C3AB ' Now that we have utf-8, convert it to iso-8859-1: cc.FromCharset = "utf-8" cc.ToCharset = "iso-8859-1" isoBytes = cc.ConvertData(utf8Bytes) ' Look at the results: inHexStr = cc.LastInputAsHex outHexStr = cc.LastOutputAsHex Response.Write "<b>utf-8 to iso-8859-1 conversion:</b><br>" Response.Write "Input: " + inHexStr + "<br>" Response.Write "Output: " + outHexStr + "<p>" ' This is the output: ' Input: 65C3 A8C3 A9C3 AAC3 AB ' Output: 65E8 E9EA EB ' The iso-8859-1 charset encoding is 1-byte/character, and we can see that it ' is correct in the output. ' This ASP page's Response Code page is iso-8859-1, so we can emit the bytes ' directly with Response.BinaryWrite... Response.Write "This is emitted with Response.BinaryWrite: " Response.BinaryWrite isoBytes Response.Write "<p>" %> </body> </html> |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.