JavaScript编程实现ASC转汉字及互转方法
实例讲述了JavaScript实现ASC转汉字及汉字转ASC的方法。
我们常常在编写Java程序时会用到Properties文件,把一些Message等信息放在Properties文件,但是我们看到的都是一些编码。
Struts1.1b2的例子中的本国资源文件经过Unicode编码的,所以你如果要和它的运行一样,也必须将你的ApplicationResources_zh进行Unicode编码。有以下两种方法:
①使用jdk的native2ascii工具。
native2ascii 功能说明:
将含有本地编码字符(既非 Latin1 又非 Unicode 字符)的文件转换为 Unicode 编码字符的文件。
语法:native2ascii [options] [inputfile [outputfile]]
补充说明:Java 编译器和其它 Java 工具只能处理含有 Latin-1 和/或 Unicode 编码(udddd 记号)字符的文件。native2ascii 将含有其它字符编码的文件转换成含 Latin-1 和/或 Unicode 编码字符的文件。
若省略 outputfile,则使用标准输出设备输出。
此外,如果也省略 inputfile,则使用标准输入设备输入。
命令选项:
-reverse 执行相反的操作:将含 Latin-1 和/或 Unicode 编码字符的文件转换成含本地编码字符的文件。
-encoding[encoding_name] 指定转换过程使用的编码名称。缺省的编码从系统属性 file.encoding 中得到。
应用实例:native2ascii -encoding GBK ApplicationResources.properties ApplicationResources_zh_CN.properties
我的做法:
1、将存有英文信息的ApplicationResource.properties复制一份,重命名为a(为了减少dos命令的长度);
2、使用Editplus编辑文件a,写入中文信息;3、在Dos窗口中,切换到文件a所在目录,运行:native2ascii a ApplicationResource_zh.properties
②我用Javascript写了一个编码和汉字互转的工具。仅供参考。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<!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> |