The most convenient Unicode Conversion Method in Java

Source: Internet
Author: User

Transferred from:Http://www.ceapet.com/blog/index.php? Option = com_content & view = article & id = 162: javaunicode & catid = 38: Java & Itemid = 64

 

 

Use native2ascii on the command line interface
1. Convert Chinese characters to UNICODE:
C:/program files/Java/jdk1.5.0 _ 04/bin> native2ascii
Test
/U6d4b/u8bd5
2. Convert Unicode to Chinese characters:
C:/program files/Java/jdk1.5.0 _ 04/bin> native2ascii-reverse
/U6d4b/u8bd5
Test

 

Java native2ascii.exe is a good transcoding tool in the bin directory. If your properties contains Chinese characters, it must be converted to Unicode. then copy the properties containing Chinese characters to the bin directory, and then enter the bin directory in cmd, input: native2ascii-encoding UTF-8 Language-ext_zh_CN.properties.native Language-ext_zh_CN.properties is OK. The Language-ext_zh_CN.properties is a transcoded file.

 
During Java Development, some garbled characters may occur, or files that cannot be correctly identified or read, such as common validator verification Message Resources (properties) the file must undergo Unicode re-encoding. The reason is that Java uses Unicode by default, while our computer system uses GBK encoding. It is necessary to convert the system encoding to the correct encoding identified by Java.
1. Introduction to native2ascii: native2ascii is a tool provided by Sun Java SDK. It is used to convert other text files (such as *. txt, *. ini, *. properties, *. Java, etc.) into unicode encoding. The reason for transcoding is that the program is internationalized. Unicode encoding definition: Unicode (Uniform Code, universal code, Single Code) is a character encoding used on a computer. It sets a unified and unique binary code for each character in each language to meet the requirements of cross-language and cross-platform text conversion and processing. R & D started in December 1990 and officially announced in December 1994. With the enhancement of computing power, Unicode has been popularized for more than a decade since its launch. (Declaration: unicode encoding definition comes from the Internet ).
2. Obtain native2ascii: Signature.
3. native2ascii command line naming format:
Native2ascii-[Options] [inputfile [outputfile]
Note:
-[Options]: indicates the command switch. Two options are available.
-Reverse: converts unicode encoding to local or specified encoding. If no encoding is specified, it is converted to local encoding.
-Encoding encoding_name: convert to the specified encoding, and encoding_name is the encoding name.
[Inputfile [outputfile]
Inputfile: The full name of the input file.
Outputfile: name of the output file. If this parameter is missing, it is output to the console.
4. Best Practice: First add the JDK bin directory to the system variable path. Create a test directory under the disk, and create a zh.txt file in the testdirectory. The file content is "lava". Open the "command line prompt" and go to the C:/test directory. Next we can follow the instructions to perform operations step by step. Pay attention to observe the encoding changes.
A: Convert zh.txtto unicode.pdf and output the file to u.txt.
Native2ascii zh.txt u.txt
Open u.txt with the content "/u7194/u5ca9 ".
B: Convert zh.txt to Unicode and output it to the console.
C:/test> native2ascii zh.txt
/U7194/u5ca9
As you can see, the console outputs "/u7194/u5ca9 ".
C: Convert zh.txtto iso8859-1133, and output the file to I .txt
Native2ascii-encoding ISO8859-1 zh.txt I .txt
Open the I .txt file with the content "/u00c8/u00db/u00d1/u00d2 ".
D: Convert the u.txtfile to the upload folder and output it to the u_nv.txt file.
Native2ascii-reverse u.txt u_nv.txt
Open the u_nv.txt file with the content "lava ".
E: Convert u.txt to local encoding and output it to the console
C:/test> native2ascii-reverse u.txt
Lava
As you can see, the console outputs "lava ".
F: Convert I .txtto strongswan and output it to I _nv.txt.
Native2ascii-reverse I .txt I _nv.txt
Open the I _nv.txt file with the content "/u00c8/u00db/u00d1/u00d2 ". The results are exactly the same before and after transcoding. That is to say, the name is not transferred, or the idea is confused ..
G: Convert I .txtto gbk.txt and output it to I _gbk.txt.
Native2ascii-reverse-encoding GBK I .txt I _gbk.txt
Open the I _gbk.txt file with the content "/u00c8/u00db/u00d1/u00d2 ". The results are exactly the same before and after transcoding. That is to say, the name is not transferred, or the idea is confused.
H: transcode u_nv.txt to the local GBK and output it to the console.
C:/test> native2ascii-reverse-encoding ISO8859-1 I .txt
Lava
From this point of view, the target reached, I .txt FOR THE ISO8859-1, converted to local encoding after the content is "lava ". From this point, we should realize that the encoding specified by-encoding in the native2ascii-reverse command is the source file encoding format. In the native2ascii command, the encoding specified by-encoding is the (generated) encoding format of the target file. This is very important! Remember !!
Create a new file 12a.txt with the content "12axyz ". Let's take a look at the encoding of pure letters and numbers.
I: Convert the TXT file of pure numbers to Unicode
Native2ascii 12a.txt 12a_nv.txt
Open the 12a_nv.txt file with the content "12axyz ".
Continue testing, convert to ISO8859-1 Code look
C:/test> native2ascii-encoding ISO8859-1 12a.txt
12 axyz
The result is still not transcoded.
From the results, we can conclude that the content before and after transcoding is the same for plain numbers and letters.
5. Conclusion: native2ascii is a very good transcoding tool and transcoding is reversible! The real meaning is not local encoding-> transcoding to ASCII code, but a common text file encoding conversion tool. There are two types of specified encoding During encoding conversion: output file encoding and input file encoding. For details, refer to the Best Practices Section.

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.