GB2312到U8码的转换有一套自己的机制的。
解决方案 »
- 自定义$()函数出错
- 吕小小 关于nodejs操作mongodb问题
- parent.document.getElementById("main").src中的parent是什么意思?
- Javascript技巧:列表CheckBox全选
- js棘手问题(在线等,下班前揭贴)
- 求用法的出处
- 急,多行select问题。不能获得焦点。高分求快速解决
- 请高人看下我的这段代码,对象无法移动
- 求在aspx页下一段javascript的浮动广告(随滚动条一起上下移动)代码,试了有效马上给分,谢谢
- 如何在客户端判断?
- 如何让嵌入的媒体播放器显示首帧图像就暂停?
- 直接把text的文字加到dropdownlist中(在线急)
var s = UTF8Encode("中文")
alert(s)
alert(UTF8Decode(s))function URLDecode(strURL)
{
return unescape(strURL);
}function URLEncode(strURL)
{
var strSpecialUrl = " <>\"#%{}|^~[]`'&?+";
var strEncode="";
var i, j, chUrl, iCode, iCodeBin, num;
var tempBin;
var leadingzeros; strURL+="";
for (i=0; i<strURL.length; i++) {
chUrl = strURL.charAt(i);
iCode = chUrl.charCodeAt(0);
if (iCode<=parseInt("0x7F")) {
if (strSpecialUrl.indexOf(chUrl)!=-1) {
//chUrl is a special character that needs to be Url encoded
strEncode+="%"+iCode.toString(16).toUpperCase();
} else {
//otherwise chrUrl is normal
strEncode+=chUrl;
}
} else {
leadingzeros="";
iCodeBin=iCode.toString(2)
if (iCode<=parseInt(0x7FF)) {
//glyph is represented by two chars //check leading zeros on iCodeBin (should be 11 digits)
for (j=11; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="110"+iCodeBin.substr(0,5);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(5,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
} else {
if (iCode<=parseInt(0xFFFF)) {
//glyph is represented by three chars //check leading zeros on iCodeBin (should be 16 digits)
for (j=16; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="1110"+iCodeBin.substr(0,4);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(4,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(10,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
} else {
if (iCode<=parseInt(0x1FFFFF)) {
//glyph is represented by four chars //check leading zeros on iCodeBin (should be 21 digits)
for (j=21; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="11110"+iCodeBin.substr(0,3);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(3,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(9,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(15,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
} else {
if (iCode<=parseInt(0x3FFFFFF)) {
//glyph is represented by five chars //check leading zeros on iCodeBin (should be 26 digits)
for (j=26; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="111110"+iCodeBin.substr(0,2);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(2,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(8,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(14,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(20,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
} else {
if (iCode<=parseInt(0x7FFFFFFF)) {
//glyph is represented by six chars //check leading zeros on iCodeBin (should be 31 digits)
for (j=31; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="1111110"+iCodeBin.substr(0,1);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(1,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(7,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(13,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(19,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(25,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
}
}
}
}
}
}
}
return strEncode;
}function Utf8ToUnicode(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 < nTotalChars)
{
iCode = strUtf8.charCodeAt(nOffset);
if ((iCode & 0x80) == 0) // 1 byte.
{
if ( nRemainingBytes < 1 ) // not enough data
break; bstr += String.fromCharCode(iCode & 0x7F);
nOffset ++;
nRemainingBytes -= 1;
}
else if ((iCode & 0xE0) == 0xC0) // 2 bytes
{
iCode1 = strUtf8.charCodeAt(nOffset + 1);
if ( nRemainingBytes < 2 || // not enough data
(iCode1 & 0xC0) != 0x80 ) // invalid pattern
{
break;
} bstr += String.fromCharCode(((iCode & 0x3F) << 6) | ( iCode1 & 0x3F));
nOffset += 2;
nRemainingBytes -= 2;
}
else if ((iCode & 0xF0) == 0xE0) // 3 bytes
{
iCode1 = strUtf8.charCodeAt(nOffset + 1);
iCode2 = strUtf8.charCodeAt(nOffset + 2);
if ( nRemainingBytes < 3 || // not enough data
(iCode1 & 0xC0) != 0x80 || // invalid pattern
(iCode2 & 0xC0) != 0x80 )
{
break;
} bstr += String.fromCharCode(((iCode & 0x0F) << 12) |
((iCode1 & 0x3F) << 6) |
(iCode2 & 0x3F));
nOffset += 3;
nRemainingBytes -= 3;
}
else // 4 or more bytes -- unsupported
break;
} if (nRemainingBytes != 0)
{
// bad UTF8 string.
return "";
} return bstr;
}function UTF8Decode(str)
{
return Utf8ToUnicode(URLDecode(str))
}function UTF8Encode(str)
{
return URLEncode(str)
}</script>