当前位置:   article > 正文

java ucs2转utf8_Unicode,UTF8,GB2312,UCS2,GBK之间的转换

ucs2在线编码转换

Unicode,UTF8,GB2312,UCS2,GBK之间的转换

平时用到的几种编码格式转换。平时用的不是很多。但是在做短信协议的时候,就经常遇到了。这段时间做短信平台接口,总结了几个,也不是很全。

//

//把汉字格式化为%HH

int URLEncode(LPCTSTR pInBuf,LPTSTR szOut)

{

LPBYTE pInTmp,pOutTmp;

pInTmp = (LPBYTE)pInBuf;

pOutTmp = (LPBYTE)szOut;

while (*pInTmp){

if(isalnum(*pInTmp)){

*pOutTmp++ = *pInTmp;

}else{

if(isspace(*pInTmp)){

*pOutTmp++ =

'+';

}else{ *pOutTmp++ =

'%'; *pOutTmp++ =

toHex(*pInTmp>>4); *pOutTmp++ =

toHex(*pInTmp&0xF); }

}

pInTmp++;

}

*pOutTmp = '\0';

return (int)(pOutTmp-(LPBYTE)szOut);

}

///

// Unicode字符 转换成UTF-8编码

LPCTSTR UnicodeToUTF8Char(LPTSTR pOut,WCHAR wcText)

{

// 注意 WCHAR高低字的顺序,低字节在前,高字节在后

LPTSTR pchar = (LPTSTR)&wcText;

pOut[0] = (0xE0 | ((pchar[1] & 0xF0)

>> 4));

pOut[1] = (0x80 | ((pchar[1] & 0x0F)

<< 2)) + ((pchar[0] &

0xC0) >> 6);

pOut[2] = (0x80 | (pchar[0] & 0x3F));

pOut[3] = '\0';

return pOut;

}

//GB2312字符串转为UTF-8编码

LPCTSTR GB2312ToUTF8(LPTSTR pUTF8Out,LPCTSTR pGB2312Input, int

GB2312Len)

{

CHAR buf[4]; LPCTSTR lpReturn,pGB2312Cursor,pGB2312InputEnd;

WCHAR wcBuffer;

lpReturn = (LPCTSTR)pUTF8Out;

pGB2312Cursor = (LPTSTR)pGB2312Input;

pGB2312InputEnd= pGB2312Cursor + GB2312Len;

while( pGB2312Cursor

//如果ANSII直接复制就可以

if( *pGB2312Cursor>0 ){

*pUTF8Out++ =

*pGB2312Cursor++; }else{ ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,pGB2312Cursor,2,&wcBuffer,1);

memcpy( pUTF8Out,

UnicodeToUTF8Char(buf,wcBuffer), 3 );

pGB2312Cursor += 2;

pUTF8Out +=

3; }

}

*pUTF8Out = '\0';

return lpReturn;

}

int UTF8ToGB(const char* str,char *out)

{

WCHAR *strSrc;

TCHAR *szRes;

int len;

//获得临时变量的大小

int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);

strSrc = new WCHAR[i+1];

MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);

//获

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/924881
推荐阅读
相关标签
  

闽ICP备14008679号