SoFunction
Updated on 2025-02-28

js Chinese characters to Unicode, Unicode to Chinese characters, ASCII to Unicode, Unicode to Unicode, Unicode to ASCII, Chinese to X function code

Recently, I saw that many online tools have some code conversion codes, which we can use in many cases. Here I will share with you these materials.

Unicode introduction

Unicode (Unicode, Unicode, Single Code) is a character encoding used on a computer.
Unicode is created to solve the limitations of traditional character encoding schemes. It sets a unified and unique binary encoding for each character in each language to meet the requirements of cross-language and cross-platform text conversion and processing.
Unicode is a character encoding scheme developed by international organizations that can accommodate all the texts and symbols in the world. Unicode uses the number 0-0x10FFFF to map these characters, which can accommodate up to 1114112 characters, or 1114112 code bits. Code bits are numbers that can be assigned to characters.
Unicode: Among the five planes defined so far, the 0th plane (BMP) is the most important, and its encoded Chinese characters range is: 4E00-9FBFCJK Unified Ideographs (CJK Unified Ideographs)

Introduction to ASCII

ASCII is a computer encoding system based on Latin letters. It is mainly used to display modern English and other Western European languages.
It is the most common single-byte encoding system today and is equivalent to the international standard ISO/IEC 646.
0-127 is the range of 7-bit ASCII codes and is an international standard. As for Chinese characters, the range of ascii codes used in different character sets is different. Commonly used Chinese character sets include GB2312-80, GBK, Big5, unicode, etc.
GB_2312 character set is currently the most commonly used Chinese character encoding standard. In this standard, each Chinese character is represented by 2 bytes, and the ascii code of each byte is 161-254 (hexadecimal A1-FE), the first byte corresponds to the 1-94 area code, and the second byte corresponds to the 1-94 bits of the bit code.

Introduction to ASCII

native2ascii is a tool provided by sun java sdk. Used to convert the encoding of other text-class files (such as *.txt, *.ini, *.properties, *.java, etc.) into Unicode encoding. The reason for transcoding is the internationalization of the program.
After installing jdk, if you install it on Windows, then in the jdk installation directory, there will be a bin directory, which is the native2ascii Chinese to unicode tool.
The naming format of the command line of native2ascii: native2ascii -[options] [inputfile [outputfile]].
For example: native2ascii: will convert to Unicode encoding and output the file to.

In this tool, Chinese characters and Unicode conversion is developed in PHP, supports hexadecimal and decimal representation, and can convert Chinese characters and Unicode together; by default, hexadecimal is used.

Functions that are required for the following functions

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

Chinese characters to Unicode

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

Unicode to Chinese characters, ASCII to Unicode

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

Unicode conversion ASCII

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

Chinese Converter&#XXXX

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

Complete testable code

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

&lt;style&gt;
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;
}
&lt;/style&gt;
Provide a Chinese characterUnicodeTransfer to each other、 ASCIIandUnicodeTransfer to each other的在线工具,Convenient to help you solve the problem of garbled Chinese。

	  &lt;div class='divider'&gt;&lt;/div&gt;
	  &lt;textarea  name="input_area" placeholder="Stick the Unicode or Ascii characters to be processed" value=""&gt; - I&lt;/textarea&gt;
			&lt;div class='row'&gt;
				&lt;button onclick="javascript:a('CONVERT_FMT2');"&gt;Chinese charactersUnicode&lt;/button&gt;
				&lt;button onclick="javascript:a('RECONVERT');"&gt;UnicodeTransfer to Chinese characters&lt;/button&gt;		
				&lt;button onclick="javascript:a('RECONVERT')"&gt;ASCIIConvertUnicode&lt;/button&gt;
				&lt;button onclick="javascript:a('CONVERT_FMT3');"&gt;UnicodeConvertASCII&lt;/button&gt;
				&lt;button onclick="javascript:a('CONVERT_FMT1');"&gt;中文Convert&amp;#XXXX&lt;/button&gt;
			&lt;/div&gt;
&lt;textarea name="output_area"  onclick="();" placeholder="Unicode or Ascii characters after processing" value=""&gt;&lt;/textarea&gt;

After seeing this, I will share with you a useful method of converting &#number encoding into strings

&lt;script&gt;
//Take; numbervar str="&amp;#104;&amp;#116;&amp;#116;&amp;#112;&amp;#115;&amp;#58;&amp;#47;&amp;#47;&amp;#119;&amp;#119;&amp;#119;&amp;#46;&amp;#106;&amp;#98;&amp;#53;&amp;#49;&amp;#46;&amp;#110;&amp;#101;&amp;#116;&amp;#47;&amp;#97;&amp;#114;&amp;#116;&amp;#105;&amp;#99;&amp;#108;&amp;#101;&amp;#47;&amp;#49;&amp;#46;&amp;#104;&amp;#116;&amp;#109;";
//No semicolonvar str2="&amp;#104&amp;#116&amp;#116&amp;#112&amp;#115&amp;#58&amp;#47&amp;#47&amp;#119&amp;#119&amp;#119&amp;#46&amp;#106&amp;#98&amp;#53&amp;#49&amp;#46&amp;#110&amp;#101&amp;#116&amp;#47&amp;#97&amp;#114&amp;#116&amp;#105&amp;#99&amp;#108&amp;#101&amp;#47&amp;#49&amp;#46&amp;#104&amp;#116&amp;#109";
function uncode(str) {
return (/&amp;#(x)?([^&amp;]{1,5});?/g, function (a, b, c) {
return (parseInt(c, b ? 16 : 10));
})
}

(uncode(str));
("&lt;br&gt;");
(uncode(str2));
&lt;/script&gt;

This is the introduction here, you can test it more specifically.

Online Unicode/Chinese conversion tool