Use Native2ascii for Chinese garbled, transcoding operation for NATIVE2ASCII processing properties file

Source: Internet
Author: User

Native2ascii is a transcoding tool provided by the Sun Java SDK to convert other text class files (such as *.txt, *.ini, *.properties, *.java, and so on) to Unicode encoding.

1. How do I get native2ascii.exe?

After installing the JDK, if you are installing on Windows, there will be a bin directory under the JDK installation directory, which will have the Native2ascii.exe tools we need.

2. Native2ascii.exe How to use: Java transcoding tool NATIVE2ASCII official version rating: 10.0 Category: Programming Auxiliary Size: 20KB language: Chinese
View more information >>

Syntax format for the command:

Native2ascii-[options] [inputfile [OutputFile]]

Description

-[options]: Represents a command switch with two options to choose from

-reverse: Converts the Unicode encoding to local or to the specified encoding, without specifying the encoding, and will be converted to local encoding.

-encoding Encoding_name: Converts to the specified encoding, Encoding_name is the encoded name.

[Inputfile [OutputFile]]

Inputfile: Indicates the full name of the input file.

OutputFile: Output file name. If this parameter is missing, it is output to the console.

Example: Copy files that need to be internationalized (e.g.) to the C:\Program Files\java\jdk1.6.0_31\bin directory and apply the Native2ascii.exe tool for transcoding.

Enter the DOS command window with the cmd command to do the following:

After successful transcoding via the NATIVE2ASCII command, such as:

Note:Native2ascii.exe is a relatively simple transcoding tool, and transcoding is reversible.

Try the following command:

Native2ascii-reverse New.txt Yan.txt

Results:

Native2ascii-encoding GBK old.txt New.txt

Results:

Java code Implementation of the JDK tool Native2ascii.exe

It is useful to share a Java-implemented Native2ascii.exe conversion tool.

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, +) << 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));

}

}

Use Native2ascii for Chinese garbled, transcoding operation for NATIVE2ASCII processing properties file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.