提醒你注意事项:因为手机上的UNICODE数据高低字节是互换的,所以你必须在转换前或转换后额外地互换高低字节
下面有MO转换函数,请参数:
int CXXXXXX:
rocessMo(int& nDcs, LPSTR srcBuff, int srclen, LPSTR objBuff, int& objlen)
{
switch(nDcs)
{
case 4://binary
if(srclen>0 && srcBuff[0] == '+')
{
strncpy(objBuff, srcBuff, srclen);
nDcs = 0;
objlen = srclen;
}
else
{
objlen = 0;
}
break;
case 8://unicode
if(srclen>0 && srcBuff[0] == '+')//stk card
{
char *start = strstr(srcBuff, " ");
if(!start || start<srcBuff || start>=srcBuff+srclen) {objlen = 0; return 0;}//error
strncpy(objBuff, srcBuff, start-srcBuff);
objBuff[start-srcBuff] = 0;
PWSTR pStart = (PWSTR)(start+1);
for(UINT loop=0; loop<wcslen(pStart); loop++,pStart++)
{
char * p = (char*)pStart;
char c = p[0];
p[0] = p[1];
p[1] = c;
}
int mlen = WideCharToMultiByte(CP_ACP,0,pStart,-1,NULL,0,NULL,NULL);
char *pStr= (char *)malloc(mlen);
WideCharToMultiByte(CP_ACP, 0, pStart, -1, pStr, mlen, NULL, NULL);
pStr[mlen] = 0;
strcat(objBuff, " ");
strcat(objBuff, pStr);
//objlen = start-srcBuff+mlen+1;
free(pStr);
}
else//normal ucs2
{
for(int loop=0; loop<srclen; loop++)//注意,这里高低字节互换
{
char c = srcBuff[loop];
srcBuff[loop] = srcBuff[loop+1];
srcBuff[++loop] = c;
}
int objlen = WideCharToMultiByte(CP_ACP,0, (PWSTR)srcBuff,-1,NULL,0,NULL,NULL);
WideCharToMultiByte(CP_ACP, 0, (PWSTR)srcBuff, -1, objBuff, objlen, NULL, NULL);
}
objlen = strlen(objBuff);
nDcs = 15;
break;
default:
strncpy(objBuff, srcBuff, srclen);
objlen = srclen;
break;
}
return 0;
}