1. 程式人生 > >Unicode,UTF8,GB2312,UCS2,GBK之間的轉換

Unicode,UTF8,GB2312,UCS2,GBK之間的轉換

void UCS2toUTF8(unsigned short *ucs2, int count, char *utf8)
{
    unsigned short unicode;
    unsigned char bytes[4] = {0};
    int nbytes = 0;
    int i = 0, j = 0;
    int len=0;
 
    if((ucs2 != NULL) && (utf8 != NULL))
    {
        if(count  == 0)
        {
            len = 0;
        }
        else
        {
   for (i=0; i<count; i++)
   {
    unicode = ucs2[i];
    
    if (unicode < 0x80)
    {
     nbytes = 1;
     bytes[0] = unicode;
    }
    else if (unicode < 0x800)
    {
     nbytes = 2;
     bytes[1] = (unicode & 0x3f) | 0x80;
                    bytes[0] = ((unicode << 2) & 0x1f00 | 0xc000) >> 8;
    }
    else
    {
     nbytes = 3;
     bytes[2] = (unicode & 0x3f) | 0x80;
     bytes[1] = ((unicode << 2) & 0x3f00 | 0x8000) >> 8;
                    bytes[0] = ((unicode << 4) & 0x0f0000 | 0xe00000) >> 16;
    }
    
    for (j=0; j<nbytes; j++)
    {
     utf8[len] = bytes[j];
     len++;
                }
            }
        }
       
        utf8[len] = '/0';
    }
}