native2ascii是sun java sdk提供的一個(gè)轉(zhuǎn)碼工具, 用來(lái)將別的文本類文件(比如 *.txt, *.ini, *.properties, *.java 等等)編碼轉(zhuǎn)為Unicode編碼。
1. 如何獲取 native2ascii.exe?
安裝了jdk后,假如你是在windows上安裝,那么在jdk的安裝目錄下,會(huì)有一個(gè)bin目錄, 該目錄下就有我們所需要的native2ascii.exe 工具。
2. native2ascii.exe使用方法:
命令的語(yǔ)法格式:
native2ascii -[options] [inputfile [outputfile]]
說(shuō)明:
-[options]:表示命令開(kāi)關(guān),有兩個(gè)選項(xiàng)可供選擇
-reverse:將Unicode編碼轉(zhuǎn)為本地或者指定編碼,不指定編碼情況下,將轉(zhuǎn)為本地編碼。
-encoding encoding_name:轉(zhuǎn)換為指定編碼,encoding_name為編碼名稱。
[inputfile [outputfile]]
inputfile:表示輸入文件全名。
outputfile:輸出文件名。如果缺少此參數(shù),將輸出到控制臺(tái)。
舉例: 把需要進(jìn)行國(guó)際化的文件(如下圖)拷貝到 C:\Program Files\Java\jdk1.6.0_31\bin 目錄下, 應(yīng)用native2ascii.exe工具進(jìn)行轉(zhuǎn)碼。
通過(guò)cmd命令進(jìn)入DOS命令窗口進(jìn)行如下操作:
通過(guò)native2ascii命令轉(zhuǎn)碼成功后,如下圖:
備注:native2ascii.exe是一個(gè)應(yīng)用比較簡(jiǎn)單的轉(zhuǎn)碼工具,并且轉(zhuǎn)碼是可逆的。
試試如下命令:
native2ascii -reverse new.txt yan.txt
結(jié)果:
native2ascii -encoding gbk old.txt new.txt
結(jié)果:
java代碼實(shí)現(xiàn)的jdk工具native2ascii.exe
分享個(gè)java實(shí)現(xiàn)的native2ascii.exe的轉(zhuǎn)換工具,很有用。
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));
}
}