utf-8编码转换成gb2312
更新时间:2006年09月23日 00:00:00 作者:
[code]
function chinesefromutf8url(strutf8)
{
var bstr = "";
var noffset = 0;
// processing point on strutf8
if( strutf8 == "" )
return "";
strutf8 = strutf8.tolowercase();
noffset = strutf8.indexof("%e");
if( noffset == -1 )
return strutf8;
while( noffset != -1 )
{
bstr += strutf8.substr(0, noffset);
strutf8 = strutf8.substr(noffset, strutf8.length - noffset);
if( strutf8 == "" ¦ ¦ strutf8.length
return bstr;
bstr += utf8codetochinesechar(strutf8.substr(0, 9));
strutf8 = strutf8.substr(9, strutf8.length - 9);
noffset = strutf8.indexof("%e");
}
return bstr + strutf8;
}
function unicodefromutf8(strutf8)
{
var bstr = "";
var ntotalchars = strutf8.length; // total chars to be processed.
var noffset = 0; // processing point on strutf8
var nremainingbytes = ntotalchars; // how many bytes left to be converted
var noutputposition = 0;
var icode, icode1, icode2; // the value of the unicode.
while (noffset
{
icode = strutf8.charcodeat(noffset);
if ((icode & 0x80) == 0) // 1 byte.
{
if ( nremainingbytes
break;
bstr += string.fromcharcode(icode & 0x7f);
noffset ++;
nremainingbytes -= 1;
}
else if ((icode & 0xe0) == 0xc0) // 2 bytes
{
icode1 = strutf8.charcodeat(noffset + 1);
if ( nremainingbytes
(icode1 & 0xc0) != 0x80 ) // invalid pattern
{
break;
}
bstr += string.fromcharcode(((icode & 0x3f) <
noffset += 2;
nremainingbytes -= 2;
}
else if ((icode & 0xf0) == 0xe0) // 3 bytes
{
icode1 = strutf8.charcodeat(noffset + 1);
icode2 = strutf8.charcodeat(noffset + 2);
if ( nremainingbytes
(icode1 & 0xc0) != 0x80 ¦ ¦ // invalid pattern
(icode2 & 0xc0) != 0x80 )
{
break;
}
bstr += string.fromcharcode(((icode & 0x0f) <
((icode1 & 0x3f) <
(icode2 & 0x3f));
noffset += 3;
nremainingbytes -= 3;
}
else // 4 or more bytes -- unsupported
break;
}
if (nremainingbytes != 0)
{
// bad utf8 string.
return "";
}
return bstr;
}
function utf8codetochinesechar(strutf8)
{
var icode, icode1, icode2;
icode = parseint("0x" + strutf8.substr(1, 2));
icode1 = parseint("0x" + strutf8.substr(4, 2));
icode2 = parseint("0x" + strutf8.substr(7, 2));
return string.fromcharcode(((icode & 0x0f) <
((icode1 & 0x3f) <
(icode2 & 0x3f));
}
alert(chinesefromutf8url("%e6%b5%8b%e8%af%95"))
[code]
相关文章
这篇文章主要介绍了对象不支持此属性或方法: Session.CodePage = 936 解决方法,需要的朋友可以参考下2015-07-07
asp创建访问统计代码,实例2008-10-10
在asp中,我们可以用response.cookies方法,定义cookies的值。2010-03-03
在ASP编程中使用数组...2006-10-10
个人网站如有会员注册模块+动网论坛的话,那网站要与动网论坛系统整合,实现不同Web系统之间的用户信息同步更新、登录等操作就不是件容易的事了2008-06-06
抓取网页。偶要实现实实更新天气预报。利用了XMLHTTP组件,抓取网页的指定部分,其实很多的小偷程序要更好用2012-10-10
这篇文章主要介绍了ASP模仿google suggest风格实现下拉菜单效果,需要的朋友可以参考下2015-09-09
获取当前月份距离以前某个时间的月份数,asp都是用DateDiff函数来实现2012-04-04
NextRecordset 和 GetRows 双簧合奏...2006-09-09
利用ActiveX控件InetCtls.Inet在ASP中实现新闻小偷...2007-02-02
最新评论