convert Chinese to unicode with java

There is a exe file native2ascii.exe in jdk’s bin that can convert Chinese to unicode.

It can be used in cmd window.

The commond:

native2ascii [options] [inputfile [outputfile]]

OPTIONS

-reverse
Perform the reverse operation: Convert a file encoded in ISO-8859-1 with Unicode escapes to a file in any character encoding supported by the Java runtime environment.

-encoding encoding_name
Specifies the name of the character encoding to be used by the conversion procedure. If this option is not present, the default character encoding (as determined by the java.nio.charset.Charset.defaultCharset method) is used. The encoding_name string must be the name of a character encoding that is supported by the Java runtime environment – see the Supported Encodings document.

-Joption
Pass option to the Java virtual machine, where option is one of the options described on the reference page for the java application launcher. For example, -J-Xms48m sets the startup memory to 48 megabytes.

example:

  • create a file and input Chinese, save as d:\a.txt in UTF-8 format.

eg.: 国民党

  • use native2ascii.exe to convert:

c:\Java\jdk1.6.0\bin>native2ascii.exe -encoding utf8 d:\a.txt d:\b.txt

the converted file is b.txt, open it

eg. result: \ufeff\u56fd\u6c11\u515a

  • use the ext to convert back:

c:\Java\jdk1.6.0\bin>native2ascii.exe -reverse  d:\b.txt d:\c.txt

the result was input to c.txt.

eg. result: \ufeff国民党

  • the commond will add \ufeff as prefix. it can be removed using replaceAll:

replaceAll(“\ufeff”, “”)

reference:

1.http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/native2ascii.html

2.http://www.mkyong.com/java/java-convert-chinese-character-to-unicode-with-native2ascii/

This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s