天天看點

UTF-8, Unicode, GB2312格式串轉換之C語言版---轉

UTF-8, Unicode, GB2312格式串轉換之C語言版

      (申明:此文章屬于原創,若轉載請表明作者和原處連結 )      

    return 0;

}

      最後就是搜尋算法了,前面已經排好序了,現在我們把排好序的碼表放在我們真正需要的.h檔案中。大家應該猜我用什麼算法搜尋了吧,二分法。

#define CODE_TABLE_SIZE 21791

//這個表是死的,是以就直接用宏表示長度,不用每次都用size,不過這樣可能對移植性不好。

u16 SearchCodeTable(u16 unicodeKey)

{

    int first = 0;

    int end = CODE_TABLE_SIZE - 1;

    int mid = 0;

    while (first <= end)

    {

        mid = (first + end) / 2;

        if (code_table[mid].unicode == unicodeKey)

        {

            return code_table[mid].gb;

        }

        else if (code_table[mid].unicode > unicodeKey)

        {

            end = mid - 1;

        }

        else 

        {

            first = mid + 1;

        }

    }

    return 0;

}

      到此,已經能夠将UTF8串轉換成GB2312了。是一長串哦,而不是單個漢字的編碼轉換。

http://blog.csdn.net/huazi0072009/archive/2010/08/11/5803504.aspx