JavaScript实现中文与Unicode互转及ASCII编码转换详解

作者:工具松鼠

时间:2026-10-30

浏览:238

本文详细介绍如何使用JavaScript实现中文字符与Unicode编码的双向转换,以及ASCII码与Unicode的相互转换。文章提供了完整的代码示例,涵盖中文转Unicode、Unicode还原中文、ASCII转Unicode及Unicode转ASCII等常见场景,并附带HTML测试界面代码,帮助开发者快速掌握字符编码处理技巧。

文章目录

编码转换工具概述

目前许多在线工具都提供了编码转换功能,这类功能在实际开发中应用广泛。本文将分享相关的技术资料和实现方法。

Unicode与ASCII编码基础

Unicode编码概述

Unicode(统一码、万国码、单一码)是一种用于计算机字符编码的标准。它旨在解决传统字符编码方案的局限性,为每种语言中的每个字符分配统一且唯一的二进制编码,从而满足跨语言、跨平台的文本转换与处理需求。Unicode是国际组织制定的字符编码方案,能够容纳全球所有文字和符号。它使用数字0到0x10FFFF映射这些字符,最多可容纳1114112个字符,即拥有1114112个码位。码位指分配给字符的数字标识。在Unicode定义的五个平面中,第0平面(BMP)最为重要,其中文汉字编码范围为4E00-9FBFCJK统一表意符号(CJK Unified Ideographs)。

ASCII编码概述

ASCII 是基于拉丁字母的一套电脑编码系统。它主要用于显示现代英语和其他西欧语言。 它是现今最通用的单字节编码系统,并等同于国际标准 ISO/IEC 646。 0-127 是 7 位 ASCII 码的范围,是国际标准。至于汉字,不同的字符集用的 ascii 码的范围也不一样,常用的汉字字符集有 GB2312-80,GBK,Big5,unicode 等。 GB_2312 字符集是目前最常用的汉字编码标准。在这个标准中,每个汉字用 2 个字节来表示,每个字节的 ascii 码为 161-254 (16 进制 A1 - FE),第一个字节 对应于 区码的 1-94 区,第二个字节 对应于位码的 1-94 位。

其他编码工具介绍

native2ascii是Sun Java SDK提供的一个工具,用于将其他文本类文件(如*.txt、*.ini、*.properties、*.java等)编码转换为Unicode编码。进行转码的主要原因是为了实现程序的国际化。安装JDK后,若在Windows系统上安装,可在JDK安装目录下的bin目录中找到native2ascii.exe,即native2ascii中文转Unicode工具。native2ascii的命令行格式为:native2ascii -[options] [inputfile [outputfile]]。例如:native2ascii zh.txt u.txt,表示将zh.txt转换为Unicode编码,输出文件为u.txt。

JavaScript编码转换实现

本工具中的汉字与Unicode转换功能采用PHP开发,支持十六进制和十进制表示形式,能够实现中文汉字与Unicode的双向转换;默认情况下采用十六进制格式。

以下函数为后续转换功能所共用的基础函数。


function left_zero_4(str) {
			if (str != null && str != '' && str != 'undefined') {
				if (str.length == 2) {
					return '00' + str;
				}
			}
			return str;
		}

中文汉字转Unicode实现


function unicode(str){
			var value='';
			for (var i = 0; i < str.length; i++) {
				value += '\\u' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16));
			}
			return value;
		}
		function left_zero_4(str) {
			if (str != null && str != '' && str != 'undefined') {
				if (str.length == 2) {
					return '00' + str;
				}
			}
			return str;
		}

Unicode转中文汉字及ASCII转换Unicode实现


function reconvert(str){ 
			str = str.replace(/(\\u)(\w{1,4})/gi,function($0){ 
				return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{1,4})/g,"$2")),16))); 
			}); 
			str = str.replace(/(&#x)(\w{1,4});/gi,function($0){ 
				return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g,"$2"),16)); 
			}); 
			str = str.replace(/(&#)(\d{1,6});/gi,function($0){ 
				return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g,"$2"))); 
			}); 
			
			return str; 
		}

Unicode转换ASCII实现


		function unicode1(str){ 
			var value='';
			for (var i = 0; i < str.length; i++)
				value += '&#' + str.charCodeAt(i) + ';';
			return value;
		} 

中文转换&#XXXX格式实现


		function ascii(str){ 
			var value='';
			for (var i = 0; i < str.length; i++) {
				value += '\&#x' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16))+';';
			}
			return value;
		} 

完整测试代码与扩展功能

完整的可测试代码示例


		<script type="text/javascript">
		function a(pChoice){
			var inputEle = document.getElementById('input_area');
			var outputEle = document.getElementById('output_area');
			switch(pChoice){ 
				case "CONVERT_FMT1":
					outputEle.value = ascii(inputEle.value);
					break; 
				case "CONVERT_FMT2":
					outputEle.value = unicode(inputEle.value);
					break; 
				case "CONVERT_FMT3":
					outputEle.value = unicode1(inputEle.value);
					break; 
				case "RECONVERT":
					outputEle.value = reconvert(inputEle.value);
					break; 
			} 
		} 
		function ascii(str){ 
			var value='';
			for (var i = 0; i < str.length; i++) {
				value += '\&#x' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16))+';';
			}
			return value;
		} 
		function unicode(str){
			var value='';
			for (var i = 0; i < str.length; i++) {
				value += '\\u' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16));
			}
			return value;
		}
		function left_zero_4(str) {
			if (str != null && str != '' && str != 'undefined') {
				if (str.length == 2) {
					return '00' + str;
				}
			}
			return str;
		}
		function unicode1(str){ 
			var value='';
			for (var i = 0; i < str.length; i++)
				value += '&#' + str.charCodeAt(i) + ';';
			return value;
		} 
		function reconvert(str){ 
			str = str.replace(/(\\u)(\w{1,4})/gi,function($0){ 
				return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{1,4})/g,"$2")),16))); 
			}); 
			str = str.replace(/(&#x)(\w{1,4});/gi,function($0){ 
				return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g,"$2"),16)); 
			}); 
			str = str.replace(/(&#)(\d{1,6});/gi,function($0){ 
				return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g,"$2"))); 
			}); 
			
			return str; 
		}
		</script>

<style>
textarea {
 width: 100%;
 height: 200px;
 resize:vertical;
 border: 1px solid #CCC;
 /*border-radius:8px;*/
 padding:4px;
 box-shadow: 2px 2px 5px #d3d6da;
 -moz-box-shadow: 2px 2px 5px #d3d6da;
}
</style>
提供一个中文汉字Unicode互转、 ASCII与Unicode互转的在线工具,方便帮助你解决中文的乱码问题。

	  <div class='divider'></div>
	  <textarea id="input_area" name="input_area" placeholder="贴入要处理的Unicode或Ascii字符" value="">jb51.net - 本站</textarea>
			<div class='row'>
				<button onclick="javascript:a('CONVERT_FMT2');">中文汉字转Unicode</button>
				<button onclick="javascript:a('RECONVERT');">Unicode转中文汉字</button>		
				<button onclick="javascript:a('RECONVERT')">ASCII转换Unicode</button>
				<button onclick="javascript:a('CONVERT_FMT3');">Unicode转换ASCII</button>
				<button onclick="javascript:a('CONVERT_FMT1');">中文转换&#XXXX</button>
			</div>
<textarea name="output_area" id="output_area" onclick="this.select();" placeholder="处理之后的Unicode或Ascii字符" value=""></textarea>

阅读至此,本站小编再为大家分享一个实用的方法,可将&#数字编码转换为字符串。


<script>
//带;号
var str="&#104;&#116;&#116;&#112;&#115;&#58;&#47;&#47;&#119;&#119;&#119;&#46;&#106;&#98;&#53;&#49;&#46;&#110;&#101;&#116;&#47;&#97;&#114;&#116;&#105;&#99;&#108;&#101;&#47;&#49;&#46;&#104;&#116;&#109;";
//不带分号
var str2="&#104&#116&#116&#112&#115&#58&#47&#47&#119&#119&#119&#46&#106&#98&#53&#49&#46&#110&#101&#116&#47&#97&#114&#116&#105&#99&#108&#101&#47&#49&#46&#104&#116&#109";
function uncode(str) {
return str.replace(/&#(x)?([^&]{1,5});?/g, function (a, b, c) {
return String.fromCharCode(parseInt(c, b ? 16 : 10));
})
}

document.write(uncode(str));
document.write("<br>");
document.write(uncode(str2));
</script>

以上内容介绍完毕,具体应用建议读者多进行测试验证。

在线Unicode/中文转换工具

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章