源码在此!没有调用任何操作系统函数,不过码表不给了,太大了!
老是有人发信要我解决 Unicode 到 GB 的编码问题,我在此贴代码算了,免得再找我
程序在FressBSD/Linux上调试通过,Windows 用户只要修改几个变量类型和头文件即可
说明:函数名称
int strUnicode2GB(char *strSourcer, char *strDest,int n)
转换Unicde字符串到GB码,返回汉字数
输入( Unicode 源串,GB2312/ASCII混合编码串,Unicode字节数 必须是偶数!!!)
#include <sys/types.h>;
const unsigned short int Unicode_GB2312[][2] =
{
/*Unicode ,GB||ASCII ,MEMO*/
0x0000,0x0040,/* 0 COMMERCIAL AT @ */
0x0001,0x00A3,/* 1 POUND SIGN £ */
0x0002,0x0024,/* 2 DOLLAR SIGN $ */
0x0003,0x00A5,/* 3 YEN SIGN ¥ */
0x0004,0x00E8,/* 4 LATIN SMALL LETTER E WITH GRAVE è */
.............
.............
码表太长了 请尽量通过 Google 查询,多的是
或者通过 [email protected] 向我索取
0xFF5C,0xA3FC,/* '|' ->; 65372 */
0xFF5D,0xA3FD,/* '}' ->; 65373 */
0xFF5E,0xA1AB,/* '~' ->; 65374 */
0xFFE0,0xA1E9,/* '¢' ->; 65504 */
0xFFE1,0xA1EA,/* '£' ->; 65505 */
0xFFE3,0xA3FE,/* ' ̄' ->; 65507 */
0xFFE5,0xA3A4,/* '¥' ->; 65509 */
};
u_int16_t Unicode2GBcode(u_int16_t iUnicode)
{
int i,j,n;
switch (iUnicode){
case 0x0002:
return 0x24;
break;
case 0x000a:
return 0xa;
break;
case 0x000d:
return 0xd;
break;
case 0x0040:
return 0xA1;
break;
}
if ((iUnicode>;=0x20&&iUnicode<=0x5a)||(iUnicode>;=0x61&&iUnicode<=0x7a)) return iUnicode;
for (i=0,j=0,n=sizeof(Unicode_GB2312)/sizeof(Unicode_GB2312[0])-1;n>;0;n>;>;=1,++j){
if(Unicode_GB2312[0]==iUnicode) return Unicode_GB2312[1];
if (j>;1){
if(Unicode_GB2312[i-1][0]==iUnicode) return Unicode_GB2312[i-1][1];
if(Unicode_GB2312[i+1][0]==iUnicode) return Unicode_GB2312[i+1][1];
}
if (Unicode_GB2312[0]<iUnicode) i=i+n;
else i=i-n;
}
if(Unicode_GB2312[0]==iUnicode) return Unicode_GB2312[1];
if(Unicode_GB2312[i-1][0]==iUnicode) return Unicode_GB2312[i-1][1];
if(Unicode_GB2312[i+1][0]==iUnicode) return Unicode_GB2312[i+1][1];
return 0; //转换不成功
}
/*转换Unicde字符串到GB码,返回汉字数*/
int strUnicode2GB(const char *strSourcer,const char *strDest,int n)
{
char cTmp;
u_int16_t hz,tmphz;
char *pSrc;
char *pDest;
int i;
for (i=0,pSrc=strSourcer,pDest=strDest;n>;0;n-=2,pSrc+=2,++i,++pDest){
hz=0;
hz=*pSrc<<8|(*(pSrc+1)&0x00FF);
tmphz=Unicode2GBcode(hz);
if (!tmphz||tmphz>;0x7F&&tmphz<0xFF){
*pDest='.';
continue;
}
else if (tmphz>;0x00&&tmphz<=0x7F){
cTmp=tmphz;
*pDest=cTmp;
}
else{
cTmp=tmphz;
*pDest=(tmphz>;>;=8);
++pDest;
*pDest=cTmp;
}
}
*pDest='/0';
return i;
}