Home  >  Article  >  Web Front-end  >  JavaScript method to convert ASC to Chinese characters and Chinese characters to ASC_javascript skills

JavaScript method to convert ASC to Chinese characters and Chinese characters to ASC_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:18:372095browse

The example in this article describes the method of converting ASC to Chinese characters and converting Chinese characters to ASC using JavaScript. Share it with everyone for your reference, the details are as follows:

We often use Properties files when writing Java programs and put some Message and other information in the Properties files, but all we see are some encodings.

The domestic resource files in the Struts1.1b2 example are Unicode encoded, so if you want to run the same as it, you must also Unicode encode your ApplicationResources_zh. There are two methods:

①Use jdk’s native2ascii tool.

native2ascii function description:

Convert files containing natively encoded characters (that are neither Latin1 nor Unicode characters) to files with Unicode encoded characters.

Syntax: native2ascii [options] [inputfile [outputfile]]

Additional Note: The Java compiler and other Java tools can only process files containing Latin-1 and/or Unicode encoded (udddd notation) characters. native2ascii Converts files containing other character encodings to files containing Latin-1 and/or Unicode encoded characters.

If outputfile is omitted, the standard output device is used for output.

Also, if inputfile is also omitted, input from the standard input device is used.

Command Options:

-reverse does the opposite: converts a file containing Latin-1 and/or Unicode-encoded characters into a file containing natively-encoded characters.
-encoding[encoding_name] Specifies the encoding name used by the conversion process. The default encoding is obtained from the system property file.encoding.

Application example: native2ascii -encoding GBK ApplicationResources.properties ApplicationResources_zh_CN.properties

My approach:

1. Copy ApplicationResource.properties that contains English information and rename it to a (in order to reduce the length of the dos command);

2. Use Editplus to edit file a and write Chinese information; 3. In the Dos window, switch to the directory where file a is located and run: native2ascii a ApplicationResource_zh.properties

②I used Javascript to write a tool for converting encoding and Chinese characters . For reference only.

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
  <TITLE>ASC←→NATIVEツール@杭州の王徳封</TITLE>
  <METANAME="Generator"CONTENT="EditPlus">
  <METANAME="Author"CONTENT="szwangdf@163.com">
  <METANAME="Keywords"CONTENT="ASC←→NATIVEツール">
  <METANAME="Description"CONTENT="ASC←→NATIVEツール">
  <script language="javascript">
    function native2ascii(){
      var regexp=/[^/x00-/xff]/g;
      var n=document.getElementById("native").value;
      var a=n;
      while(m=regexp.exec(n)){
        a=a.split(m[0]).join(escape(m[0]).split("%").join("/"));
      }
      document.getElementById("ascii").value=a;
    }
    function ascii2native() {
      var a=document.getElementById("ascii").value;
      var n=a;
      var n=unescape(n.split("/").join("%"));
      document.getElementById("native").value=n;
    }
  </script>
</HEAD>
<BODY>
  <h1>ASC←→NATIVEツール</h1>
  コード :<br>
  <textarea id="ascii" rows="10" cols="100"></textarea><br>
  漢字:            
  <input type="button" id="back"  value="コード→漢字  ↓↓↓" onclick="ascii2native()"/>
      
  <input type="button" id="convert" value="漢字→コード  ↑↑↑" onclick="native2ascii()"/>
  <br>
  <textarea id="native" rows="10" cols="100"></textarea>
</BODY>
</HTML>

I hope this article will be helpful to everyone in JavaScript programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn