ant 的native2ascii unicode转换不正确问题

03-10-08 mirage

用ant的native2ascii将含中文的js文件转为ascii,再转为utf8和unicode:

GBK->ascii->UTF8

ascii->Unicode

UTF8转换正确,但是转为Unicode时,第一个汉字的escaped unicode编码总是不能转为unicode编码。

<project name="encodetrans" default="main" basedir=".">
    <target name="gbk2ascii" >
      <native2ascii src="d:/test/" encoding="GBK" includes="*1.js" dest="d:/test/test1" />
   </target>
   <target name="ascii2utf8" depends="gbk2ascii">
      <native2ascii src="d:/test/test1" encoding="UTF-8" reverse="true" includes="*1.js" ext=".utf" dest="d:/test/test1" />
   </target>
   <target name="ascii2unicode" depends="gbk2ascii">
      <native2ascii src="d:/test/test1" encoding="Unicode" reverse="true" includes="*.js" ext=".uni" dest="d:/test/test1" />
   </target>
   
   <target name="main" depends="ascii2utf8,ascii2unicode">
   </target>
</project>
<p>

用Jdk的native2ascii.exe,也存在同样的问题

native2ascii -encoding Unicode -reverse a1.js a1.uni1

请问哪位仁兄知道如何解决

我用的是win2000 server,jdk1.4.1,ant版本换了三个都一样(1.4、1.5.1、1.5.2)

1
Romain
2003-10-08 22:51

看看这个,希望对你有帮助!

private void convertToUnicode(File src, File dest) {

try {

BufferedReader br = new BufferedReader(new FileReader(src));

BufferedWriter bw = new BufferedWriter(new FileWriter(dest));

String str, tmp;

char c;

int i, j, k;

StringBuffer sb = new StringBuffer(1000);

while ((str = br.readLine()) != null) {

sb.setLength(0);

for (i = 0; i < str.length(); i++) {

c = str.charAt(i);

if (c > 255) {

sb.append("\\u");

j = (c >>> 8);

tmp = Integer.toHexString(j);

if (tmp.length() == 1) sb.append("0");

sb.append(tmp);

j = (c & 0xFF);

tmp = Integer.toHexString(j);

if (tmp.length() == 1) sb.append("0");

sb.append(tmp);

}

else {

sb.append(c);

}

}

bw.write(sb.toString(), 0, sb.length());

bw.newLine();

}

br.close();

bw.close();

} catch(IOException err) {

err("Error processing file " + src + ", reason: " + err.getMessage());

}

}