java做的一个将中文转换成Unicode码的工具类【转载】做个标记,明天研究下

这两天在使用RBManager(一个开源工具,用于多国化字符转化)工具的时候觉得很不方便,有的时候只需要知道中文对应的unicode码是多少,不需要这么麻烦的操作,所以就自己写了一个工具,专门用于将中文转换成unicode码。
1.工具代码如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
 1 package ben;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStreamReader;
 5 import java.io.UnsupportedEncodingException;
 6 
 7 public class Native2Ascii {
 8 static String java_bin_path = “D:\\jdk1.4.2_03\\bin”;
 9 // 存放中文的文本文件
10 static String source_file = “C \\a.txt”;
11 
12 public static void main(String[] args) {
13 String result = getUnicodeString(source_file);
14 System.out.println(result);
15 }
16 
17 private static String getUnicodeString(String destFileName) {
18 StringBuffer tempSb = new StringBuffer();
19 try {
20 Process p = Runtime.getRuntime().exec(java_bin_path+ “\\native2ascii.exe ” + destFileName);
21 InputStreamReader child_in = new InputStreamReader(p.getInputStream());
22 int c;
23 while ((c = child_in.read()) != -1) {
24 tempSb.append((char)c);
25 }
26 catch (UnsupportedEncodingException e) {
27 e.printStackTrace();
28 catch (IOException ex) {
29 ex.printStackTrace();
30 }
31 
32 return tempSb.toString();
33 }
34 }
35 
36 

其中,两个变量需要修改一下。
a1.“java_bin_path”:jdk的bin目录;
a2.“source_file”:要转换的中文所在的文本文件。
2.a.txt文本文件
中文测试
中文

3.输出结果
\u4e2d\u6587\u6d4b\u8bd5
\u4e2d\u6587

转载于:https://www.cnblogs.com/pencilsoft/archive/2009/04/16/1436872.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值