UTF-8编码转换成GB2312

{代码}
功能chinesefromutf8url(strutf8)
{
VaR BSTR = ;
无功noffset = 0;
在strutf8 / /加工点
如果(strutf8 = = )
返回;
strutf8 = strutf8.tolowercase();
noffset = strutf8.indexof(E);
如果(noffset = 1)
返回strutf8;

而(noffset!= 1)
{
strutf8.substr BSTR =(0,noffset);
strutf8 = strutf8.substr(noffset,strutf8.length - noffset);
如果(strutf8 = =strutf8。长度| |<9) / /坏的字符串
返回BSTR;

BSTR = utf8codetochinesechar(strutf8.substr(0, 9));
strutf8 = strutf8.substr(9,strutf8.length - 9);
noffset = strutf8.indexof(E);
}

返回BSTR + strutf8;
}

功能unicodefromutf8(strutf8)
{
VaR BSTR = ;
无功ntotalchars = strutf8.length; / /总字符进行处理。
无功noffset = 0; / /加工点strutf8
无功nremainingbytes = ntotalchars多字节左右; / /如何被转换
无功noutputposition = 0;
VaR的ICODE,icode1 icode2,的Unicode值。; / /

而(noffset < ntotalchars)
{
strutf8.charcodeat ICODE =(noffset);
如果((ICODE 0x80)= = 0) / / 1字节。
{
如果(nremainingbytes<1) / /没有足够的数据
打破;

BSTR = string.fromcharcode(ICODE 0x7f);
noffset + +;
nremainingbytes = 1;
}
else if((ICODE 0xE0)= 0xC0) / / 2字节
{
icode1 = strutf8.charcodeat(noffset + 1);
如果(nremainingbytes<2 | | / /没有足够的数据
(icode1 0xC0)= 0x80模式 / /无效)!
{
打破;
}

string.fromcharcode BSTR =(((ICODE 0x3f)<6)|(icode1 0x3f));
noffset = 2;
nremainingbytes = 2;
}
else if((ICODE 0xf0)= 0xE0) / / 3字节
{
icode1 = strutf8.charcodeat(noffset + 1);
icode2 = strutf8.charcodeat(noffset + 2);
如果(nremainingbytes<3 | | / /没有足够的数据
(icode1 0xC0)!= 0x80 | | / /无效的模式
(icode2 0xC0)!= 0x80)
{
打破;
}

string.fromcharcode BSTR =((ICODE 0x0f)(12)|
((icode1 0x3f)<6)|
(icode2 0x3f));
noffset = 3;
nremainingbytes = 3;
}
其他或更多-不支持的字节 4
打破;
}

如果(nremainingbytes!= 0)
{
UTF8字符串。
返回;
}

返回BSTR;
}

功能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));

返回((ICODE 0x0f)(string.fromcharcode<12)|
((icode1 0x3f)<6)|
(icode2 0x3f));
}
警报(chinesefromutf8url(% % % % e6 B5 8b E8 % AF % 95))
{代码}