在J2ME实现UTF-8转换为Unicode编码的方法
UTF-8就是Unicode Transformation Format-8,是Unicode的一种变换编码格式。本文介绍了UTF-8转换为Unicode编码的方法,给出了在J2ME环境下的实现。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UTF-8就是Unicode Transformation Format-8,是Unicode的一种变换编码格式。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UTF-8 有以下特性:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UCS 字符 U+0000 到 U+007F (ASCII) 被编码为字节 0x00 到 0x7F (ASCII 兼容). 这意味着只包含 7 位 ASCII 字符的文件在 ASCII 和 UTF-8 两种编码方式下是一样的.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
所有 >U+007F 的 UCS 字符被编码为一个多个字节的串, 每个字节都有标记位集. 因此, ASCII 字节 (0x00-0x7F) 不可能作为任何其他字符的一部分.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
表示非 ASCII 字符的多字节串的第一个字节总是在 0xC0 到 0xFD 的范围里, 并指出这个字符包含多少个字节. 多字节串的其余字节都在 0x80 到 0xBF 范围里. 这使得重新同步非常容易, 并使编码无国界, 且很少受丢失字节的影响.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
可以编入所有可能的 231个 UCS 代码
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UTF-8 编码字符理论上可以最多到 6 个字节长, 然而 16 位 BMP 字符最多只用到 3 字节长.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Bigendian UCS-4 字节串的排列顺序是预定的.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
字节 0xFE 和 0xFF 在 UTF-8 编码中从未用到.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
下列字节串用来表示一个字符. 用到哪个串取决于该字符在 Unicode 中的序号.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
U-00000000 - U-0000007F:
0xxxxxxx
U-00000080 - U-000007FF:
110xxxxx 10xxxxxx
U-00000800 - U-0000FFFF:
1110xxxx 10xxxxxx 10xxxxxx
U-00010000 - U-001FFFFF:
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
U-00200000 - U-03FFFFFF:
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
U-04000000 - U-7FFFFFFF:
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
从上表还可以看出,UTF-8每个编码字符都不可能以“10”开头,“10”是以连接符的形式出现在后面的编码字节开头。因此UTF-8编码在存储和传输时是不容易出错的。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
下面是UTF-8到Unicode的编码转换代码(J2ME环境下的实现):UTFC2UniC方法包含了编码转换逻辑。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
* 将UTF-8字节数据转化为Unicode字符串
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
* @param utf_data byte[] - UTF-8编码字节数组
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
* @param len int - 字节数组长度
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
* @return String - 变换后的Unicode编码字符串
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
*/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public static String UTF2Uni(byte[] utf_data, int len)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
StringBuffer unis = new StringBuffer();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
char unic = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int ptr = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int cntBits = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for(;ptr < len;)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cntBits = getCntBits(utf_data[ptr]);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(cntBits == -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
++ptr;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
continue;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(cntBits == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
unic = UTFC2UniC(utf_data, ptr, cntBits);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
++ptr;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
unic = UTFC2UniC(utf_data, ptr, cntBits);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ptr += cntBits;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
unis.append(unic);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return unis.toString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
* 将指定的UTF-8字节组合成一个Unicode编码字符
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
* @param utf byte[] - UTF-8字节数组
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
* @param sptr int - 编码字节起始位置
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
* @param cntBits int - 编码字节数
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
* @return char - 变换后的Unicode字符
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
*/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public static char UTFC2UniC(byte[] utf, int sptr, int cntBits)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Unicode <-> UTF-8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
U-00000000 - U-0000007F: 0xxxxxxx
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
*/
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int uniC = 0; // represent the unicode char
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte firstByte = utf[sptr];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int ptr = 0; // pointer 0 ~ 15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// resolve single byte UTF-8 encoding char
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(cntBits == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return (char) firstByte;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// resolve the first byte
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
firstByte &= (1 << (7 - cntBits)) - 1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// resolve multiple bytes UTF-8 encoding char(except the first byte)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for(int i = sptr + cntBits - 1; i > sptr; --i)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte utfb = utf[i];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
uniC |= (utfb & 0x3f) << ptr;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ptr += 6;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
uniC |= firstByte << ptr;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return (char)uniC;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 根据给定字节计算UTF-8编码的一个字符所占字节数
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// UTF-8规则定义,字节标记只能为0或2~6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private static int getCntBits(byte b)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int cnt = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (b == 0) return -1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for(int i = 7; i >= 0; --i)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (((b >> i) & 0x1) == 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
++cnt;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return (cnt > 6 || cnt == 1) ? -1 : cnt;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
参考资料:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
《UTF-8 and Unicode FAQ》—— http://www.linuxforum.net/books/UTF-8-Unicode.html
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)