有的手机不支持utf8. 需要时直接copy。
UTF-8 Decoder:
String decode(byte[] b,int start,int len) {
StringBuffer s = new StringBuffer();
int end = start + len;
for (int i = start; i < end; ) {
UTF-8 Decoder:
String decode(byte[] b,int start,int len) {
StringBuffer s = new StringBuffer();
int end = start + len;
for (int i = start; i < end; ) {
byte b = b[i++];
if ( (b >> 7) == 0)
s.append( (char) b);
else if ( (b >> 5) == (byte) 0xfe)
s.append((char)(((b & 0x1f)<<6) | (b[i++] & 0x3f)));
s.append( (char) b);
else if ( (b >> 5) == (byte) 0xfe)
s.append((char)(((b & 0x1f)<<6) | (b[i++] & 0x3f)));
else if ( (b >> 4) == (byte) 0xfe)
s.append((char)(((b & 0xf)<<12) | ( (b[i++] & 0x3f) << 6) | (b[i++] &0x3f)));
s.append((char)(((b & 0xf)<<12) | ( (b[i++] & 0x3f) << 6) | (b[i++] &0x3f)));
}
return new String(s);
}
UTF-8 Encoder:
byte[] utf8_encode(String s) {
int len = 0;
int strlen = s.length();
for (int i = 0; i < strlen; i++) {
UTF-8 Encoder:
byte[] utf8_encode(String s) {
int len = 0;
int strlen = s.length();
for (int i = 0; i < strlen; i++) {
char c = s.charAt(i);
if (c < 128) len++;
else if (c < 2048) len += 2;
else len += 3;
}
byte[] b = new byte[len];
len = 0;
for (int i = 0; i < strlen; i++) {
char c = s.charAt(i);
if (c < 128)
b[len++] = (byte) c;
else if (c < 2048) {
b[len++]=(byte)((c>>6)|192);
b[len++]=(byte)((c&63)|128);
}
else {
b[len++]=(byte)((c>>12)|224);
b[len++]=(byte)((c>>12)|224);
b[len++]=(byte)((c&63) |128);
}
}
return b;
}
b[len++]=(byte)((c&63)|128);
}
else {
b[len++]=(byte)((c>>12)|224);
b[len++]=(byte)((c>>12)|224);
b[len++]=(byte)((c&63) |128);
}
}
return b;
}