近日开发中遇到半角与全角转换的问题,当时想了很多方法来实现可是总不近人意,最终用了最简单的数组方式来实现(人笨没办法),大家如果有更好的方法可以贴出来哈。
源码如下:
#define ZEN_MAX_LEN 2056
int FormatKaToZen(char* pDestination ,char* pSource)
{
char *pTempDest = pDestination;
char *pTempSource = pSource;
char* pTemp = NULL;
int iTemp = 0;
char szDestination[ZEN_MAX_LEN] = "";
char szZen[3] = "";
char szHan[256][3] = {{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},
{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},
{" "},{"!"},{"”"},{"#"},{"$"},{"%"},{"&"},{"’"},{"("},{")"},{"*"},{"+"},{"、"},{"-"},{"."},{"/"},
{"0"},{"1"},{"2"},{"3"},{"4"},{"5"},{"6"},{"7"},{"8"},{"9"},{":"},{";"},{"<"},{"="},{">"},{"?"},
{"@"},{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},{"G"},{"H"},{"I"},{"J"},{"K"},{"L"},{"M"},{"N"},{"O"},
{"P"},{"Q"},{"R"},{"S"},{"T"},{"U"},{"V"},{"W"},{"X"},{"Y"},{"Z"},{"["},{"¥"},{"]"},{"^"},{"_"},
{"’"},{"a"},{"b"},{"c"},{"d"},{"e"},{"f"},{"g"},{"h"},{"i"},{"j"},{"k"},{"l"},{"m"},{"n"},{"o"},
{"p"},{"q"},{"r"},{"s"},{"t"},{"u"},{"v"},{"w"},{"x"},{"y"},{"z"},{"{"},{"|"},{"}"},{"~"},{""},
{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},
{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},
{""},{"。"},{"「"},{"」"},{"、"},{"・"},{"ヲ"},{"ァ"},{"ィ"},{"ゥ"},{"ェ"},{"ォ"},{"ャ"},{"ュ"},{"ョ"},{"ッ"},
{"ー"},{"ア"},{"イ"},{"ウ"},{"エ"},{"オ"},{"カ"},{"キ"},{"ク"},{"ケ"},{"コ"},{"サ"},{"シ"},{"ス"},{"セ"},{"ソ"},
{"タ"},{"チ"},{"ツ"},{"テ"},{"ト"},{"ナ"},{"ニ"},{"ヌ"},{"ネ"},{"ノ"},{"ハ"},{"ヒ"},{"フ"},{"ヘ"},{"ホ"},{"マ"},
{"ミ"},{"ム"},{"メ"},{"モ"},{"ヤ"},{"ユ"},{"ヨ"},{"ラ"},{"リ"},{"ル"},{"レ"},{"ロ"},{"ワ"},{"ン"},{"″"},{"°"},
{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},
{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""},{""}};
for(pTempSource;*pTempSource!='\0';*pTempSource++)
{
iTemp = (int)*pTempSource;
if(iTemp >= 32 && iTemp <= 126)
{
strncpy(szZen,szHan[iTemp],3);
strcat(szDestination,szZen);
continue;
}
else if(iTemp >= -95 && iTemp <= -33)
{
iTemp = iTemp + 256;
strncpy(szZen,szHan[iTemp],3);
strcat(szDestination,szZen);
continue;
}
else
{
strncat(szDestination,pTempSource,2);
pTempSource++;
continue;
}
}
strcpy(pDestination, szDestination);
return 0;
}
posted on 2007-09-20 13:46
云翼 阅读(1800)
评论(5) 编辑 收藏 引用 所属分类:
算法