字节转换

一、宽字节转窄字节

char* wstr2asc(const short *pSrc, char *pDest)
{
char *pTmp=pDest;
char ctmp=0;
short stmp=0;

if (NULL==pSrc)return NULL;

while (*pSrc)
{
ctmp=*pSrc;
stmp=*pSrc;

if (ctmp<0)
{
*pTmp++=stmp;
*pTmp|=(stmp>>=8);
}
else
{
*pTmp=*pSrc;
}
pSrc++;
pTmp++;
}
*pTmp=’\0′;

return pDest;
}

int wstrlen(const short *pSrc)
{
short *pTmp=(short*)pSrc;

while (*pTmp&&pTmp++);

return pTmp-pSrc;
}

char* wstr2asc(const short *pSrc, const int iSrcLen, char *pDest, const int iDestLen)
{
char *pTmp=pDest;
char ctmp=0;
short stmp=0;
int i=0;
int len=wstrlen(pSrc);
if (NULL==pSrc)return NULL;

len=len>iSrcLen?iSrcLen:len;
len=(len*2)>iDestLen?iDestLen:(len*2);
while (*pSrc && i<len)
{
ctmp=*pSrc;
stmp=*pSrc;

if (ctmp<0)
{
*pTmp++=stmp;
*pTmp|=(stmp>>=8);
i++;
if (i>=len)
{
break;
}
}
else
{
*pTmp=*pSrc;
}
pSrc++;
pTmp++;
i++;
}
*pTmp=’\0′;

return pDest;
}

二、窄字节转宽字节

short* asc2wstr(const char *pSrc,short *pDest)
{
short *pTmp = pDest;
short tmp = 0;

if (NULL==pSrc)return NULL;

while(*pSrc)
{
if(*pSrc<0)
{
*pTmp=*(pSrc+1);
*pTmp<<=8;
tmp=*pSrc;
*pTmp|=(tmp&=0x00ff);
pSrc++;
}
else
{
*pTmp=*pSrc;
}
pSrc++;
pTmp++;
}
*pTmp&=0;

return pDest;
}

short* asc2wstr(const char *pSrc, const int iSrcLen, short* pDest, const int iDestLen)
{
short *pTmp = pDest;
short tmp=0;
int len=0;
int i=0;

if (NULL==pSrc)return NULL;

len=strlen(pSrc);
len=len>iSrcLen?iSrcLen:len;
len=len>iDestLen?iDestLen:len;

while(*pSrc && i<len)
{
if(*pSrc<0)
{
*pTmp=*(pSrc+1);
*pTmp<<=8;
tmp=*pSrc++;
*pTmp|=(tmp&=0x00ff);
i++;
if(i>=len)
{
*pTmp&=0x00FF;
pTmp++;
break;
}
}
else
{
*pTmp=*pSrc;
}
pSrc++;
pTmp++;
i++;
}
*pTmp&=0;

return pDest;
}

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注