如何用Javascript 将汉字转换为gb2312编码?

来源:百度知道 编辑:UC知道 时间:2024/05/18 06:03:55
javascript用encodeURI(str)可以直接将汉字转换成utf-8编码,但是想通过一段代码(需要兼容IE和Firefox)在客户端浏览器将汉字转换为gb2312编码字符串,求高手解答!高见可加高分200!(注意不是什么gb2312转utf-8类的)

实际上IE 5.5+,Netscape 6+,Mozilla中已经有了转换函数,即encodeURIComponent,但对于低版本的浏览器则需要一下代码。
/* ***************************
** Most of this code was kindly
** provided to me by
** Andrew Clover (and at doxdesk dot com)
** http://and.doxdesk.com/ ;
** in response to my plea in my blog at
** http://worldtimzone.com/blog/date/2002/09/24
** It was unclear whether he created it.
*/
function utf8(wide) {
var c, s;
var enc = "";
var i = 0;
while(i c= wide.charCodeAt(i++);
// handle UTF-16 surrogates
if (c>=0xDC00 %26amp;%26amp; c<0xE000) continue;
if (c>=0xD800 %26amp;%26amp; c<0xDC00) {
if (i>=wide.length) continue;
s= wide.charCodeAt(i++);
if (s<0xDC00 || c>=0xDE00) continue;
c= ((c-0xD800)&l