满意答案
qjmcspr8
2013.03.13
采纳率:54% 等级:12
已帮助:5205人
/* Copyright (C) 2010 by Xingyou Chen
*
* Test OS is Debian squeese AMD64 , source file stored in UTF-8
* If another encoding is used, this program may fail
*/
#include /* 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语言代码,找你需要的吧。
00分享举报