UTF-8與UNICODE的關係及代碼轉換

所謂“utf-8”只是UCS Transformation Format,只是UNICODE的一種表現形式,不等同於UNICODE,一般漢字在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是一種變長度的表達方式,一般UNICODE爲雙字節(指UCS2)但爲了與以前的ASCII碼兼容,ASCII爲一個字節,於是就想出了這種方法,在ASCII碼的範圍用一個字節表示,超出ASCII碼的範圍就用多字節表示,這就形成了我們上面看到的UTF-8的表示方法,這樣的好處是當UNICODE文檔中只有ASCII碼時,保存的文檔都爲一個字節,所以就是普通的ASCII文檔無異,讀入的時候也是如此,所以能與以前的ASCII文檔兼容。

至於大於ASCII碼的,就會由上面的第一字節的前幾位表示該unicode字符的長度,比如110xxxxxx前三位的二進制表示告訴我們這是個2BYTE的UNICODE字符;1110xxxx是個三位的UNICODE字符,依此類推,而首字節後面的字節都是以10開頭,見上面這是爲了與ASCII碼開頭的0區分告訴我們這是個多字節UTF-8編碼的後續位。看上面的編碼,我們將上面的x部分重新連起來組成的數值就是實際的UNICODE碼值了(排除10組成的標誌位)。

下面是個我寫的從UTF-8轉換到UNICODE真實值的程序,
編譯方法:
gcc utf82unicode.cpp -o utf82unicode -lstdc++

使用方法:
比如一個漢字‘新’字,它的UTF-8編碼爲:E696B0,爲了知道他的實際UNICODE編碼,執行如下程序,
 ./utf82unicode E696B0
unicode: 65B0
上面程序的輸出結果告訴我們UTF8:E696B0 對應UNICODE:65B0。


附錄:CPP程序utf82unicode.cpp
#include <stdio.h>
#include <string.h>

// UTF-8的unicode表示方法到unicode的值轉換函數
bool utf82unicode(unsigned int  byte[], int index, int count, int& unicode)
{
/*      for (int i=index; i < count; ++i) {
                printf("byte[%d]:%0Xn",i, byte[i]);
        }
        printf("byte[index] & 0x80: %0Xn", byte[index] & 0x80);
        printf("byte[index] & 0xE0: %0Xn", byte[index] & 0xE0);
        printf("byte[index] & 0xF0: %0Xn", byte[index] & 0xF0);
*/
        if (index >= count) return false;
        if ( (byte[index] & 0x80) == 0x0)              //  一位
        {
                unicode = byte[index];
        }
         else if ((byte[index] & 0xE0) == 0xC0) // 兩位
        {
                if (index + 1 >= count ) return false;
                unicode = (((int)(byte[index] & 0x1F)) << 6)
                        | (byte[ index + 1] & 0x3F);
        }
        else if ((byte[index] & 0xF0) == 0xE0) // 三位
        {
                if (index + 2 >= count) return false;
                unicode = (((int)(byte[index] & 0x0F)) << 12)
                        | (((int)(byte[index  + 1] & 0x3F)) << 6)
                        | (byte[index + 2] & 0x3F);
        }
         else if ((byte[index] & 0xF8) == 0xF0) // 四位
        {
                if (index + 3 >= count) return false;
                unicode = (((int)(byte[index] & 0x07)) << 18)
                        | (((int)(byte[index + 1] & 0x3F)) << 12)
                        | (((int)(byte[index + 2] & 0x3F)) << 6)
                        | (byte[index + 3] & 0x3F);
        }
         else if ((byte[index] & 0xFC) == 0xF8) // 五位
        {
                if (index + 4 >= count) return false;
                unicode = (((int)(byte[index] & 0x03)) << 24)
                        | (((int)(byte[index + 1] & 0x3F)) << 18)
                        | (((int)(byte[index + 2] & 0x3F)) << 12)
                        | (((int)(byte[index + 3] & 0x3F)) << 6)
                        | (byte[index + 4] & 0x3F);
        }
         else if ((byte[index] & 0xFE) == 0xFC) // 六位
        {
                if (index + 5 >= count) return false;
                unicode = (((int)(byte[index] & 0x01)) << 30)
                        | (((int)(byte[index + 1] & 0x3F)) << 24)
                        | (((int)(byte[index + 2] & 0x3F)) << 18)
                        | (((int)(byte[index + 3] & 0x3F)) << 12)
                        | (((int)(byte[index + 4] & 0x3F)) << 6)
                        | (byte[index + 5] & 0x3F);
        }
         else
         {
                return false;
        }
        return true;

}

bool char2digist(char in, char&out)
{
        if ('0' <= in && in <= '9')
                out = in - '0' + 0x0;
        else if ('A' <= in && in <= 'F')
                out = in - 'A' + 0xA;
        else if ('a' <= in && in <= 'f')
                out = in - 'a' + 0xa;
        else 
                return false;

        return true;

}

bool widechar2hexbyte(char* ch, int index, int count, unsigned int& byte)
{
        char h, l;
        if (index + 1 < count) {
                if (char2digist(ch[index], h) && char2digist(ch[index + 1], l))
                {
                        byte = ((unsigned int)(h << 4)) | l;
                        return true;
                }
        } else {
                if (char2digist(ch[index], l))
                {
                        byte = l;
                        return true;
                }
        }
        return false;

}

int main(int argc, char* argv[])
{
        int bi, i, len, unicode;
        char* hex;
        unsigned int bytes[10];
        if (argc < 2) {
                printf("usage: utf82unicode [hex string]n");
                return 1;
        }
        bi = 0, len = strlen(argv[1]);
//      printf("argv[1]:%s,len:%dn", argv[1], len);
        for (int i = 0; i < len && bi < 10; ++ i)
        {
                if (!widechar2hexbyte(argv[1], i++, len, bytes[bi++]))
                        return 1;
        }
        unicode = 0;
        if (utf82unicode(bytes, 0, bi, unicode))
        {
                printf("unicode: %0Xn", unicode);
                return 0;
        }
        return 1;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章