JDK中自带的native2ascii功能
1、只转换特定字符
在控制台中可以输入汉字回车后,就可以看到转移后的字符了。
Ctrl+C退出。
2、转换properties文件
native2ascii allMessages_zh_CN.input.properties allMessages_zh_CN.properties
将文件allMessages_zh_CN.input.properties编码后输出为allMessages_zh_CN.properties。
为了方便properties文件的管理,建议纯中文的配置文件用input命名。
3、反向单一properties文件
native2ascii -reverse allMessages_zh_CN.properties allMessages_zh_CN.txt
注意-reverse参数
4、批量反向所有的properties文件
JDK自带的工具native2ascii可以将uncode编码的文件转换为本地编码的文件,但是不能批量转换文件。
参考:http://ajava.org/course/java/12577.html
转载自:百度文库
什么是Unicode:
在创造Unicode之前针对各种语言有几百种编码系统,而且这些编码系统也相互冲突,给不同语言系统的电脑进行交流带来了麻烦。因为两种相同的字符在不同的编码系统可能有完全不同的意思,这些不同甚至会对电脑带来危害。于是Unicode出现了,Unicode给每个字符提供了一个唯一的数字,不论是什么平台,不论是什么程序,不论是什么语言。它真正实现了全球电脑系统的United,作为一个标准,它已经成为全球软件技术最重要的发展趋势。
为什么要把中文转换为Unicode
在互联网高速发展的今天,Unicode担当更重要的角色, 它比传统的字符编码更节省费用,使软件或者网站能够运用于不同的系统平台、语言和国家,而不需要重建,同时也保证了资料在不同系统中的完整性。所以说你只要将中文转换为Unicode,任何国家的人都能看到你想表达的真正意思,而不是乱码。
举例
中文原码(GB2312):叁肆伍陆柒捌
转为Unicode后: 叁肆伍陆柒捌网页效果为:叁肆伍陆柒捌
提示:改变网页编码为其它任何国家、语言的编码试试,始终能看见上面红色的Unicode中文。
JAVA:
将中文转为unicode 及转回中文函数
//转为unicodepublic
static void writeUnicode(final DataOutputStream out, final String value) {
try {
final String unicode = gbEncoding( value );
final byte[] data = unicode.getBytes();
final int dataLength = data.length;
System.out.println( "Data Length is: " +
dataLength );
System.out.println( "Data is: " + value );
out.writeInt( dataLength ); //先写出字符串的长度
out.write( data, 0, dataLength ); //然后写出转化后的字符串
} catch (IOException e) {
}
}
public static String gbEncoding( final String gbString ) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for( int byteIndex = 0; byteIndex < utfBytes.length; byteIndex ++ ) {
String hexB = Integer.toHexString( utfBytes[ byteIndex ] );
if( hexB.length() <= 2 ) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\\\u"
+ hexB;
}
System.out.println( "unicodeBytes is: " + unicodeBytes );
return unicodeBytes;
}
/** *//*****************************************************
* 功能介绍:将unicode字符串转为汉字
* 输入参数:源unicode字符串
* 输出参数:转换后的字符串
*****************************************************/
private String decodeUnicode( final String dataStr ) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while( start > -1 ) {
end = dataStr.indexOf( "\\\\u",
start + 2 );
String charStr = "";
if( end == -1 ) {
charStr = dataStr.substring( start + 2, dataStr.length() );
} else {
charStr = dataStr.substring( start + 2, end);
}
char letter = (char) Integer.parseInt( charStr, 16 ); // 16进制parse整形字符串。
buffer.append( new Character( letter ).toString() );
start = end;
}
return buffer.toString();
}
|