0
点赞
收藏
分享

微信扫一扫

public static byte[] convertUnicode2UTF8Byte(String instr) {

public static byte[] convertUnicode2UTF8Byte(String instr) {
if (instr == null) {
return null;
}
int len = instr.length();
byte[] abyte = new byte[len << 2];
int j = 0;
for (int i = 0; i < len; i++) {
char c = instr.charAt(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) | 0xF0);
abyte[j++] = (byte) (((c >> 12) & 0x3F) | 0x80);
abyte[j++] = (byte) (((c >> 6) & 0x3F) | 0x80);
abyte[j++] = (byte) ((c & 0x3F) | 0x80);
}
}
byte[] retbyte = new byte[j];
for (int i = 0; i < j; i++) {
retbyte[i] = abyte[i];
}
return retbyte;
}

举报

相关推荐

0 条评论