在J2ME实现UTF8转换为Unicode编码的方法

UTF-8就是Unicode Transformation Format-8,是Unicode的一种变换编码格式。本文介绍了UTF-8转换为Unicode编码的方法,给出了在J2ME环境下的实现。


UTF
-8就是Unicode Transformation Format-8,是Unicode的一种变换编码格式。 


UTF
-8 有以下特性: 

UCS 字符 U
+0000 到 U+007F (ASCII) 被编码为字节 0x00 到 0x7F (ASCII 兼容). 这意味着只包含 7 位 ASCII 字符的文件在 ASCII 和 UTF-8 两种编码方式下是一样的. 

所有 
>U+007F 的 UCS 字符被编码为一个多个字节的串, 每个字节都有标记位集. 因此, ASCII 字节 (0x00-0x7F) 不可能作为任何其他字符的一部分. 

表示非 ASCII 字符的多字节串的第一个字节总是在 
0xC0 到 0xFD 的范围里, 并指出这个字符包含多少个字节. 多字节串的其余字节都在 0x80 到 0xBF 范围里. 这使得重新同步非常容易, 并使编码无国界, 且很少受丢失字节的影响. 

可以编入所有可能的 231个 UCS 代码 

UTF
-8 编码字符理论上可以最多到 6 个字节长, 然而 16 位 BMP 字符最多只用到 3 字节长. 

Bigendian UCS
-4 字节串的排列顺序是预定的. 

字节 
0xFE 和 0xFF 在 UTF-8 编码中从未用到. 

下列字节串用来表示一个字符. 用到哪个串取决于该字符在 Unicode 中的序号. 

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 
 

从上表还可以看出,UTF
-8每个编码字符都不可能以“10”开头,“10”是以连接符的形式出现在后面的编码字节开头。因此UTF-8编码在存储和传输时是不容易出错的。


下面是UTF
-8到Unicode的编码转换代码(J2ME环境下的实现):UTFC2UniC方法包含了编码转换逻辑。


/** 

     * 将UTF-8字节数据转化为Unicode字符串 

     * 
@param utf_data byte[] - UTF-8编码字节数组 

     * 
@param len int - 字节数组长度 

     * 
@return String - 变换后的Unicode编码字符串 

     
*/
 

    
public static String UTF2Uni(byte[] utf_data, int len)

    
{

        StringBuffer unis 
= new StringBuffer();

        
char unic = 0;

        
int ptr = 0;

        
int cntBits = 0;

        
for(;ptr < len;)

        
{

            cntBits 
= getCntBits(utf_data[ptr]);

            
if(cntBits == -1)

            
{

                
++ptr;

                
continue;

            }


            
else

            
if(cntBits == 0)

            
{

                unic 
= UTFC2UniC(utf_data, ptr, cntBits);

                
++ptr;

            }


            
else

            
{

                unic 
= UTFC2UniC(utf_data, ptr, cntBits);

                ptr 
+= cntBits;

            }



            unis.append(unic);

        }



        
return unis.toString();

    }



    
/** 

     * 将指定的UTF-8字节组合成一个Unicode编码字符 

     * 
@param utf byte[] - UTF-8字节数组 

     * 
@param sptr int - 编码字节起始位置 

     * 
@param cntBits int - 编码字节数 

     * 
@return char - 变换后的Unicode字符 

     
*/
 

    
public static char UTFC2UniC(byte[] utf, int sptr, int cntBits)

    
{

       
/* 

         Unicode <-> UTF-8 

         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 

      
*/
 

        
int uniC = 0;                         //  represent the unicode char

        
byte firstByte = utf[sptr];

        
int ptr = 0;                          //  pointer 0 ~ 15


        
// resolve single byte UTF-8 encoding char 

        
if(cntBits == 0)

            
return (char) firstByte;


        
// resolve the first byte 

        firstByte 
&= (1 << (7 - cntBits)) - 1;


        
// resolve multiple bytes UTF-8 encoding char(except the first byte) 

        
for(int i = sptr + cntBits - 1; i > sptr; --i)

        
{

            
byte utfb = utf[i];

            uniC 
|= (utfb & 0x3f<< ptr;

            ptr 
+= 6;

        }


        uniC 
|= firstByte << ptr;


        
return (char)uniC;

    }



    
// 根据给定字节计算UTF-8编码的一个字符所占字节数 

    
// UTF-8规则定义,字节标记只能为0或2~6 

    
private static int getCntBits(byte b)

    
{

        
int cnt = 0;

        
if (b == 0return -1;

        
for(int i = 7; i >= 0--i)

        
{

            
if (((b >> i) & 0x1== 1)

                
++cnt;

            
else

                
break;

        }


        
return (cnt > 6 || cnt == 1? -1 : cnt;

    }




参考资料:

《UTF
-8 and Unicode FAQ》—— http://www.linuxforum.net/books/UTF-8-Unicode.html

原文地址:https://www.cnblogs.com/bluespot/p/1251134.html