Unicode encoding and Chinese conversion, unicode encoding Chinese

Source: Internet
Author: User

Unicode encoding and Chinese conversion, unicode encoding Chinese
1.1. Convert unicode encoding to Chinese

 

PublicString decodeUnicode (String src ){

Char[] In = src. toCharArray ();

CharAChar;

Char[] Out =New char[Src. length ()];

IntOutLen = 0;

IntOff = 0;

IntEnd = src. length ();

 

While(Off <end ){

AChar = in [off ++];

If(AChar = '\\'){

AChar = in [off ++];

If(AChar = 'U '){

IntValue = 0;

For(IntI = 0; I <4; I ++ ){

AChar = in [off ++];

Switch(AChar ){

Case'0 ':

Case'1 ':

Case'2 ':

Case'3 ':

Case'4 ':

Case'5 ':

Case'6 ':

Case'7 ':

Case'8 ':

Case'9 ':

Value = (value <4) + aChar-'0 ';

Break;

Case'A ':

Case'B ':

Case'C ':

Case'D ':

Case'E ':

Case'F ':

Value = (value <4) + 10 + aChar-'A ';

Break;

Case'A ':

Case'B ':

Case'C ':

Case'D ':

Case'E ':

Case'F ':

Value = (value <4) + 10 + aChar-'A ';

Break;

Default:

ThrownewIllegalArgumentException (

"Malformed \ uxxxx encoding .");

}

}

Out [outLen ++] = (Char) Value;

}Else{

If(AChar = 'T '){

AChar = '\ T ';

}Elseif(AChar = 'R '){

AChar = '\ R ';

}Elseif(AChar = 'n '){

AChar = '\ n ';

}Elseif(AChar = 'F '){

AChar = '\ F ';

}

Out [outLen ++] = aChar;

}

}Else{

Out [outLen ++] = (Char) AChar;

}

}

Return newString (out, 0, outLen );

}

 

1.2. encode Chinese characters (excluding English characters) as unicode

 

PublicString encodeUnicode (String str ){

IntIValue = 0;

String uStr = "";

For(IntI = 0; I <str. length (); I ++ ){

IValue = (Int) Str. charAt (I );

If(Ivalues <= 256 ){

UStr + = str. charAt (I );

}Else{

UStr + = "\ u" + Integer.ToHexString(IValue );

}

}

ReturnUStr;

}

1.3. encode Chinese Characters

PublicString encoding (String srcStr ){

String desStr = "";

Try{

DesStr = URLEncoder.Encode(SrcStr, "UTF-8 ");

}Catch(UnsupportedEncodingException e ){

E. printStackTrace ();

}

ReturnDesStr;

}

1.4. Chinese Decoding

 

PublicString decoding (String srcStr ){

String desStr = "";

Try{

DesStr = URLDecoder.Decode(SrcStr, "UTF-8 ");

}Catch(UnsupportedEncodingException e ){

E. printStackTrace ();

}

ReturnDesStr;

}

 

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.