native2ascii是sun java sdk提供的一个转码工具, 用来将别的文本类文件(比如 *.txt, *.ini, *.properties, *.java 等等)编码转为Unicode编码。
1. 如何获取 native2ascii.exe?
安装了jdk后,假如你是在windows上安装,那么在jdk的安装目录下,会有一个bin目录, 该目录下就有我们所需要的native2ascii.exe 工具。
2. native2ascii.exe使用方法:
查看详细信息 >>
命令的语法格式:
native2ascii -[options] [inputfile [outputfile]]
说明:
-[options]:表示命令开关,有两个选项可供选择
-reverse:将Unicode编码转为本地或者指定编码,不指定编码情况下,将转为本地编码。
-encoding encoding_name:转换为指定编码,encoding_name为编码名称。
[inputfile [outputfile]]
inputfile:表示输入文件全名。
outputfile:输出文件名。如果缺少此参数,将输出到控制台。
举例: 把需要进行国际化的文件(如下图)拷贝到 C:\Program Files\Java\jdk1.6.0_31\bin 目录下, 应用native2ascii.exe工具进行转码。
通过cmd命令进入DOS命令窗口进行如下操作:
通过native2ascii命令转码成功后,如下图:
备注:native2ascii.exe是一个应用比较简单的转码工具,并且转码是可逆的。
试试如下命令:
native2ascii -reverse new.txt yan.txt
结果:
native2ascii -encoding gbk old.txt new.txt
结果:
java代码实现的jdk工具native2ascii.exe
分享个java实现的native2ascii.exe的转换工具,很有用。
package com.zuidaima;
/**
* native2ascii.exe Java code implementation.
*
* @author
* @version 1.0
*/
public class Native2AsciiUtils {
/**
* prefix of ascii string of native character
*/
private static String PREFIX = "\\u";
/**
* Native to ascii string. It's same as execut native2ascii.exe.
*
* @param str
* native string
* @return ascii string
*/
public static String native2Ascii(String str) {
char[] chars = str.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
sb.append(char2Ascii(chars[i]));
}
return sb.toString();
}
/**
* Native character to ascii string.
*
* @param c
* native character
* @return ascii string
*/
private static String char2Ascii(char c) {
if (c > 255) {
StringBuilder sb = new StringBuilder();
sb.append(PREFIX);
int code = (c >> 8);
String tmp = Integer.toHexString(code);
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
code = (c & 0xFF);
tmp = Integer.toHexString(code);
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
return sb.toString();
} else {
return Character.toString(c);
}
}
/**
* Ascii to native string. It's same as execut native2ascii.exe -reverse.
*
* @param str
* ascii string
* @return native string
*/
public static String ascii2Native(String str) {
StringBuilder sb = new StringBuilder();
int begin = 0;
int index = str.indexOf(PREFIX);
while (index != -1) {
sb.append(str.substring(begin, index));
sb.append(ascii2Char(str.substring(index, index + 6)));
begin = index + 6;
index = str.indexOf(PREFIX, begin);
}
sb.append(str.substring(begin));
return sb.toString();
}
/**
* Ascii to native character.
*
* @param str
* ascii string
* @return native character
*/
private static char ascii2Char(String str) {
if (str.length() != 6) {
throw new IllegalArgumentException(
"Ascii string of a native character must be 6 character.");
}
if (!PREFIX.equals(str.substring(0, 2))) {
throw new IllegalArgumentException(
"Ascii string of a native character must start with \"\\u\".");
}
String tmp = str.substring(2, 4);
int code = Integer.parseInt(tmp, 16) << 8;
tmp = str.substring(4, 6);
code += Integer.parseInt(tmp, 16);
return (char) code;
}
public static void main(String[] args) {
String uni = "\u5185\u5bb9ID\u4e0d\u80fd\u4e3a\u7a7a";
System.out.println(ascii2Native(uni));
}
}