理解并解决GBK转UTF-8奇数中文乱码
先阅读:彻底搞懂字符编码http://wh.ac.cn/forum.php?mod=viewthread&tid=2918&fromuid=1
GBK编码是一个中文2个字节,而UTF-8编码是一个中文3个字节,当我们调用getBytes("UTF-8")方法时,会通过计算来增加字节,使得从GBK的2个字节变成UTF-8对应的3个字节。因此,上例3个中文输出了9个字节。
解决办法:保证字节正确才是硬道理。当调用getBytes("UTF-8")转换成字节数组后,创建ISO-8859-1编码的字符串,ISO-8859-1编码是一个字节对应一个字符,因此不会使最后一个字节错误。
案例一:Hondy.Site.API.Default.Index
if (Session.CodePage==936)
{
byte[] bytes = Encoding.UTF8.GetBytes(ret);
ret = Encoding.GetEncoding("GBK").GetString(bytes);
}
案例二:Hondy.Site.API.Login.Section调用Hondy.Site.API.Login.Menu.Options
System.Web.HttpContext.Current.Session.CodePage = CodePage;解决select.option奇数最后一个汉字显示?的问题
GBK编码网站,用AJAX向ASP传递中文参数
1、在客户端使用 encodeURIComponent 方法对中文参数进行编码,保证其采用 UTF-8 编码传输。
2、然后在ASP中使用ClassCoding.UrlDecode_JS来转换成中文录入数据库
示例:
JS:hondycms-->login->js->function.js
function getFormData($form) {
var unindexed_array = $form.serializeArray();
var indexed_array = {};
$.map(unindexed_array, function (n, i) {
indexed_array] = encodeURIComponent(n['value']);
});
return indexed_array;
}
hondycms-->login->js->contact.js
$("body").on("click", "", function (e) {
e.preventDefault();
editor.sync();
$(this).find("").val(editor.html().replaceAll("../UploadFiles/", "UploadFiles/"));
$.ajax({
type: 'POST',
data: JSON.parse(JSON.stringify($.extend({
navid: navid,
ajax: 1
}, getFormData($("form"))))),
dataType: 'json',
success: function (data) {
if (data.stat === 'ok') {
alert(common.ok);
} else {
err(data.msg);
}
}
});
})
ASP:
HCMS->Contact->Update
Content = RequestForm("Content")
Set ClassCoding = New Coding
Content = ClassCoding.UrlDecode_JS(Content)
Set ClassCoding = Nothing
页:
[1]