當前位置:編程學習大全網 - 源碼下載 - 各位大俠: 在linux下如何用C實現漢字轉unicode碼。比如:“中國”轉成“5E5D 66EF”,在線等~~

各位大俠: 在linux下如何用C實現漢字轉unicode碼。比如:“中國”轉成“5E5D 66EF”,在線等~~

/* Copyright (C) 2010 by Xingyou Chen <niatlantice@gmail.com>

*

* 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語言代碼,找妳需要的吧。

  • 上一篇:我的手機處理器是ARM920T PXA27X各方面性能怎麽樣?
  • 下一篇:Sk影視源碼建設
  • copyright 2024編程學習大全網