user1048389
user1048389

Reputation: 11

Classic ASP response.write Chinese characters in UTF-8

I am new to charsets and encoding. I don't know what I am doing wrong but the page just spits out weird codes when I want it output 你好. This is my code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=UTF-8"
Response.CodePage = 65001
Response.CharSet = "UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>
<body>
<%response.write ("你好")%>
</body>
</html>

All I can see on the screen is: ä½ å¥½ The file is saved in UTF-8 encoding as well.

Thanks in advance!

Upvotes: 1

Views: 13288

Answers (4)

user2033838
user2033838

Reputation: 153

If it helps someone. ASP declaractions always before any HTML. Incudes define session, that way never fails to encode utf-8

<%@CodePage = 65001%>
<%
Session.CodePage = 65001
Response.charset ="utf-8"
Session.LCID     = 1033 'en-US
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>
<body>
<%response.write ("你好")%>
</body>
</html>

Upvotes: 2

Kul-Tigin
Kul-Tigin

Reputation: 16950

Most likely your asp file is just UTF-8 encoded (without bom).
If it is, need to save file with BOM.
In Notepad++, Encoding > Convert to UTF-8
In Notepad, specify Encoding as UTF-8 on save dialog.

In fact, BOM for UTF-8 files is not recommended.
The point is In ASP, Response stream requires BOM implicitly.
As an experiment, try to run following page (encoded as utf-8 without bom), you'll see Response object flushes weird characters instead of the letter. However direct input will be smooth as it should be.

<%
Response.Codepage = 65001
Response.Charset = "utf-8"
Response.Write "I'm weird: 好"
%><br />I'm not weird: 好

Upvotes: 1

Sander_P
Sander_P

Reputation: 1835

You don't have to save the .asp as UTF-8 at the server side! Just save it normally (UCS-2).

Upvotes: 0

webaware
webaware

Reputation: 2841

Move your DOCTYPE declaration after the code block that sets the codepage. The codepage setting needs to happen before any output.

Upvotes: 1

Related Questions