GBK to Unicode (Java version) __java

Source: Internet
Author: User

Java version Description:

Java development, often encountered from GBK to Unicode code conversion; We often do the practice, there are about 2 kinds, one is: native2ascii; just save the file, edit the file every time, and then use the command to convert;

Another is: Eclipse plug-in properties Editor; Good usage, automatic editing; But the disadvantage is that eclipse must be used;

In order to develop convenient Ue+java, it is decided to configure properties transformation in UE and realize fast conversion (reversal);

the point :

Java Version conversion;

Because of the characteristics of Java itself, the conversion between Unicode and various encodings is easiest;

Usage: Java propertiestranslater "Convert string"

Java Propertiestranslater–reverse "Strings"

The configuration in the UE is as follows:

1. Add tool configuration to the toolbar:

2. The configuration tool is shown below, note that the parameter is%sel%, also can refer to UE document configuration;

3. Click Advanced to make the selected replacement

4. Configuration complete. The following can be in the UE, the selected text, and then press the corresponding tool shortcut keys;

You can convert the selected text to Unicode.

If you want to do a reverse conversion, just follow the above method, add the toolbar here, the command line parameter add

"-reverse" parameters can be;

/*********************************** Sample Code *********************************/

/**

*

* Propertiestranslater

*/

public class propertiestranslater{

public static void Main (string[] args) {

if (args ==null | | args[0]==null) {

SYSTEM.OUT.PRINTLN ("__ Error parameter Configuration __");

Return

}

if (args.length==2 && args[1]!=null && "-reverse". Equals (args[1)) {

String result = PROPERTIESTRANSLATER.UNICODE2GBK (args[0]);

System.out.print (result);

}else{

String result = Propertiestranslater.gbk2unicode (args[0]);

System.out.print (result);

}

}

/**

* Translate to Unicode

* @param str

* @return back to Unicode encoding

*/

public static string Gbk2unicode (String str) {

StringBuffer result = new StringBuffer ();

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

Char chr1 = (char) str.charat (i);

if (! Propertiestranslater.isneedconvert (CHR1)) {

Result.append (CHR1);

Continue

}

Result.append ("//u" + integer.tohexstring ((int) chr1));

}

return result.tostring ();

}

/**

* Unicode Transfer to Chinese

* @param str

* @return Chinese

*/

public static string UNICODE2GBK (String datastr) {

int index = 0;

StringBuffer buffer = new StringBuffer ();

while (Index<datastr.length ()) {

if (!) U ". Equals (Datastr.substring (index,index+2))) {

Buffer.append (Datastr.charat (index));

index++;

Continue

}

String charstr = "";

Charstr = datastr.substring (index+2,index+6);

Char letter = (char) integer.parseint (CHARSTR, 16);

Buffer.append (letter);

index+=6;

}

return buffer.tostring ();

}

public static Boolean Isneedconvert (char para) {

Return ((para& (0X00FF))!=para);

}

}

/*********************************** Sample Code *********************************/

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.