Some of the Chinese issues have suggested that many friends have encountered Chinese problems when they are developing, and now some of the conversion functions I collected will be announced, I hope help. Generally, Java is encoded in Unicode, and Chinese commonly used encodings with GB2312, and UTF-8, (not all entered Chinese Unicode, everyone needs to be aware). Everyone needs to be a knot in Chinese. To convert GB2312 or BIG5 into unicode: unicodestring = new string (MyString.getbytes (), "GB2312"); or unicodestring = new string (MyString.getbytes (), "BIG5"); but in a general mobile phone Different features may not support GB2312 and BIG5, and the MOTO phone I know is not supported. Therefore, the following functions may be used. (Note: Not I wrote, but it is correct)
class transCN {static public String convertUTF8String2Unicode (String instr) throws IOException {// byte [] strbytes = instr.getBytes (); int charindex = instr.length (); int actualValue; int inputValue; StringBuffer sbtemp = new StringBuffer ();
For (int i = 0; i ActualValue = -1; INPUTVALUE = Instr.Charat (i ); INPUTVALUE & = 0xFF; IF ((InputValue & 0x80) == 0) {actualue = INPUTVALUE;} else f ((InputValue & 0xF8) == 0xf0) {actualue = (InputValue & 0x1f) << 18; INT NEXTBYTE = INSTR.CHARAT (i ) & 0xff; IF ((NEXTBYTE & 0XC0)! = 0x80) Throw new oException ("Invalid UTF-8 Format"); actualValue = (NextbyTe & 0x3f) << 12; Nextbyte = instr.charat (i ) & 0xff; if (NEXTBYTE & 0XC0)! = 0x80) Throw new oException ("Invalid UTF-8 Format"); ActualValue = (NextByte & 0x3f) << 6; Nextbyte = INSTR.CHARAT (i ) & 0xff; if ((NEXTBYTE & 0XC0)! = 0x80) throw new oException ("invalid utf-8 format"); actualValue = (NextByTe & 0x3f);} else f ((InputValue) & 0xF0) == 0xE0) {actualValue = (InputValue & 0x1f) << 12; int nextbyte = instr.charat (i ) & 0xff; if ((NEXTBYTE & 0XC0)! = 0x80) throw new oException ("Invalid UTF- 8 FORMAT "); actualValue = (NextByte & 0x3f) << 6; Nextbyte = INSTR.CHARAT (i ) & 0xff; if ((NEXTBYTE & 0XC0)! = 0x80) throw new oException ("invalid utf-8 format"); actualValue = (NextByTe & 0x3f);} else f ((InputValue) & 0xe0) == 0xc0) {actualValue = (InputValue & 0x1f) << 6; INT NEXTBYTE = INSTR.CHARAT (i ) & 0xff; IF ((NEXTBYTE & 0XC0)! = 0x80) Throw new oException ("Invalid UTF-8 Format"); ActualValue = (NextByte & 0x3f);} sbtemp.append (char) actualvalue;} Return sbtemp.tostring (); Public static byte [] converTunicode2UTF8BYTE (STRING INSTR) {INT LEN = INSTR.LENGTH (); Byte [] ABYTE = New Byte [LEN << 2]; int J = 0; for (int i = 0; i IF (c <0x80) {ABYTE [J ] = (byte) C;} else if (c <0x0800) {Abyte [J ] = (BYTE) ((C >> 6) & 0x1f) | 0xc0); Abyte [J ] = (Byte) ((C & 0x3F) | 0x80);} else if (c <0x010000) {ABYTE [J ] = (Byte) ((C >> 12) & 0x0f) | 0xE0); Abyte [J ] = (Byte) (((c >> 6) & 0x3f) | 0x80); Abyte [J ] = (Byte) ((C & 0x3F) | 0x80);} else IF (c <0x200000) {Abyte [J ] = (Byte) ((C >> 18) & 0x07) | 0xF8); Abyte [J ] = (Byte) ((C >> 12) & 0x3F) | 0x80); Abyte [J ] = (((c >> 6) & 0x3f) | 0x80); Abyte [J ] = (Byte) ((c & 0x3f) | 0x80);}} Byte [] retbyte = new byte [j]; for (INT i = 0; i Public static string ISO106462UNICODE (Byte [] mybyte) {string result = new string (""); stringbuffer sb = new stringbuffer (""); try {/ * convert a string into a BYTE array * / // Byte [] mybyte = Str.getbytes ("ISO10646"); Int len = mybyte.length; For (int i = 0; i INT CH = (int) hiByte << 8; CH = CH & 0xFF00; CH = (int) LobyTe & 0xFF; Sb.append ((char) ch);} Result = new string (sb.toString ()); } Catch (Exception E) {System.out.println ("Encoding Error");} Return RESULT; Public static byte [] unicode2byte (string s) {int LEN = s.Length (); byte abyte [] = new byte [len << 1]; int J = 0; for (int i = 0; i }