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; } |