请问如何将网页中的utf-8编码转换成gb2312

thx

---------------------------------------------------------------

  1<script>   
  2  
  3function chineseFromUtf8Url(strUtf8)   
  4{   
  5var bstr = "";   
  6var nOffset = 0; // processing point on strUtf8   
  7  
  8if( strUtf8 == "" )   
  9return "";   
 10  
 11strUtf8 = strUtf8.toLowerCase();   
 12nOffset = strUtf8.indexOf("%e");   
 13if( nOffset == -1 )   
 14return strUtf8;   
 15  
 16while( nOffset != -1 )   
 17{   
 18bstr += strUtf8.substr(0, nOffset);   
 19strUtf8 = strUtf8.substr(nOffset, strUtf8.length - nOffset);   
 20if( strUtf8 == "" ¦ ¦ strUtf8.length < 9 ) // bad string   
 21return bstr;   
 22  
 23bstr += utf8CodeToChineseChar(strUtf8.substr(0, 9));   
 24strUtf8 = strUtf8.substr(9, strUtf8.length - 9);   
 25nOffset = strUtf8.indexOf("%e");   
 26}   
 27  
 28return bstr + strUtf8;   
 29}   
 30  
 31function unicodeFromUtf8(strUtf8)   
 32{   
 33var bstr = "";   
 34var nTotalChars = strUtf8.length; // total chars to be processed.   
 35var nOffset = 0; // processing point on strUtf8   
 36var nRemainingBytes = nTotalChars; // how many bytes left to be converted   
 37var nOutputPosition = 0;   
 38var iCode, iCode1, iCode2; // the value of the unicode.   
 39  
 40while (nOffset < nTotalChars)   
 41{   
 42iCode = strUtf8.charCodeAt(nOffset);   
 43if ((iCode & 0x80) == 0) // 1 byte.   
 44{   
 45if ( nRemainingBytes < 1 ) // not enough data   
 46break;   
 47  
 48bstr += String.fromCharCode(iCode & 0x7F);   
 49nOffset ++;   
 50nRemainingBytes -= 1;   
 51}   
 52else if ((iCode & 0xE0) == 0xC0) // 2 bytes   
 53{   
 54iCode1 = strUtf8.charCodeAt(nOffset + 1);   
 55if ( nRemainingBytes < 2 ¦ ¦ // not enough data   
 56(iCode1 & 0xC0) != 0x80 ) // invalid pattern   
 57{   
 58break;   
 59}   
 60  
 61bstr += String.fromCharCode(((iCode & 0x3F) << 6) ¦ ( iCode1 & 0x3F));   
 62nOffset += 2;   
 63nRemainingBytes -= 2;   
 64}   
 65else if ((iCode & 0xF0) == 0xE0) // 3 bytes   
 66{   
 67iCode1 = strUtf8.charCodeAt(nOffset + 1);   
 68iCode2 = strUtf8.charCodeAt(nOffset + 2);   
 69if ( nRemainingBytes < 3 ¦ ¦ // not enough data   
 70(iCode1 & 0xC0) != 0x80 ¦ ¦ // invalid pattern   
 71(iCode2 & 0xC0) != 0x80 )   
 72{   
 73break;   
 74}   
 75  
 76bstr += String.fromCharCode(((iCode & 0x0F) << 12) ¦   
 77((iCode1 & 0x3F) << 6) ¦   
 78(iCode2 & 0x3F));   
 79nOffset += 3;   
 80nRemainingBytes -= 3;   
 81}   
 82else // 4 or more bytes -- unsupported   
 83break;   
 84}   
 85  
 86if (nRemainingBytes != 0)   
 87{   
 88// bad UTF8 string.   
 89return "";   
 90}   
 91  
 92return bstr;   
 93}   
 94  
 95function utf8CodeToChineseChar(strUtf8)   
 96{   
 97var iCode, iCode1, iCode2;   
 98iCode = parseInt("0x" + strUtf8.substr(1, 2));   
 99iCode1 = parseInt("0x" + strUtf8.substr(4, 2));   
100iCode2 = parseInt("0x" + strUtf8.substr(7, 2));   
101  
102return String.fromCharCode(((iCode & 0x0F) << 12) ¦   
103((iCode1 & 0x3F) << 6) ¦   
104(iCode2 & 0x3F));   
105}   
106alert(chineseFromUtf8Url("%E6%B5%8B%E8%AF%95"))   
107</script>
Published At
Categories with Web编程
comments powered by Disqus