*
* Test OS is Debian squeese AMD64 , source file stored in UTF-8
* If another encoding is used, this program may fail
*/
#include <stdio.h> /* printf() from here */
int main()
{
int i = 0; /* for loop */
char orig[6] = "中國"; /* demo text, Chinese char is 3 byte long */
unsigned short dest[2]; /* two 2-byte variable */
for(i = 0; i < 2; i++)
{ /* Don't understand? See UTF-8 and Unicode encoding */
dest[i] = (orig[3*i] & 0x1F) << 12;
dest[i] |= (orig[3*i + 1] & 0x3F) << 6;
dest[i] |= (orig[3*i + 2] & 0x3F);
printf("%x", dest[i]);
}
printf("\n");
return 0;
}
====================================================
“中國”: UTF-8 e4b8ad e59bbd Unicode 4e2d 56fd
妳給出的那個編碼該是不正確的。這裏只做了UTF-8到UCS-2的轉換,
中文編碼不少,沒功夫全寫出來(也寫不全),UTF-8是我平時用的。
在開源環境中,這個根本不是問題,可以參考zh-autoconvert的源代碼:
http://ftp.de.debian.org/debian/pool/main/z/zh-autoconvert/zh-autoconvert_0.3.16.orig.tar.gz
它提供了多種中文編碼間相互轉換的C語言代碼,找妳需要的吧。