javascript,汉字转unicode|unicode转汉字

时间:2022-02-07 20:24:30
复制到一个html中就能使用,这里小记录一下。
<HTML><head><title>汉字转unicode|unicode转汉字</title><style type="text/css"><!--td {color: #FF9900}A {	COLOR: #66CC22; TEXT-DECORATION: none}A:hover {	COLOR: #ff0000; TEXT-DECORATION: none}.gree {color: #66CC33}.btn {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; color: #666666; text-align: left; text-decoration: none; display: block; overflow: visible; margin-right: 10px; margin-left: 10px; }--></style></head><body ><table width="728" height="54" border="0" align="center" cellpadding="0" cellspacing="1" >  <tr>    <td height="52"><table width="726" height="46"  border="0" cellpadding="0" cellspacing="0">      <tr align="center">          <td>            <TABLE style="MARGIN-TOP: 6px" cellSpacing=0 cellPadding=0 width="100%" border=0>              <TR>                <TD width="774" vAlign=top style="PADDING-RIGHT: 6px; PADDING-LEFT: 6px"><h2 align="center"> unicode转化汉字 汉字转化unicode编码</h2>                  <hr size="1">                  <CENTER><TEXTAREA name="source" cols="100" rows="14" id="source">\u7D2B\u964Cl\u7EA4\u5C18\u7684\u4E13\u680F\uFF0C\u6700\u7F8E\u4E0D\u8FC7\u5E73\u6DE1\uFF0C\u6700\u771F\u4E0D\u8FC7\u5E73\u51E1\u3002</TEXTAREA></CENTER>                  <BR>                  <CENTER>                    <table width="400" border="0" cellspacing="2">                      <tr>                        <td><span class="btn"><a href="javascript:action('CONVERT_FMT1')"> <strong>转化成为:<br>                        </strong>&#xxxx </a></span></td>                        <td><span class="btn"><a href="javascript:action('CONVERT_FMT2')"> <strong>转化成为unicode字符:<br>                        </strong> \uxxxx </a></span></td>                        <td><span class="btn"><a href="javascript:action('RECONVERT')"> <strong>转化为汉字:<br>                        </strong> ::WV </a></span></td>                      </tr>                    </table>                    <p>                    <div id="tt" style="display:none"></div>                    <textarea name="show2" rows="14" id="show2" cols="100"></textarea>                    </p>					<P style="MARGIN-TOP: 0px"> </P>                </CENTER></TD>                <TD noWrap width=1></TD>              </TR>            </TABLE>          </td>      </tr>    </table></td>  </tr></table><br></BODY><script language="javascript" type="text/javascript"> 	var oSource = document.getElementById("source"); 	var oShow2 = document.getElementById("show2"); 	var oTt = document.getElementById("tt"); 	function action(pChoice){ 	switch(pChoice){ 		case "CONVERT_FMT1": 		oShow2.value = ascii(oSource.value); 		break; 		case "CONVERT_FMT2": 		oShow2.value = unicode(oSource.value); 		break; 		case "RECONVERT": 		oShow2.value = reconvert(oSource.value); 		break; 		} 	} 	function ascii(str){ 		return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\&#x$2;")}); 	} 	function unicode(str){ 		return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\\u$2")}); 	} 	function reconvert(str){ 		str = str.replace(/(\\u)(\w{4})/gi,function($0){ 		return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{4})/g,"$2")),16))); 	}); 	str = str.replace(/(&#x)(\w{4});/gi,function($0){ 	return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{4})(%3B)/g,"$2"),16)); 	}); 	return str; 	} </script></HTML>