/*****************************************************************************
* class : global function
* function : to_multibyte
* description: 把unicode转换成多字节
* parameters : char* strsm -- 要转换的unicode缓冲
* int nlength -- unicode缓冲的长度
* return : 转换后的多字节字串
* input :
* output :
* history : created by rongdian monday, april 29, 2002 11:03:15
* process :
******************************************************************************/
cstring to_multibyte(char* strsm, int nlength){
uint nlen = 0;
pbyte lpszm;
pbyte lpszw = new byte[nlength];
memcpy(lpszw, strsm, nlength);
for(int i = 0; i < nlength/2; i++)
*((unsigned short*)lpszw + i) = ntohs(*((unsigned short*)lpszw + i));
nlen = widechartomultibyte(936, wc_compositecheck,
(const unsigned short*)lpszw, nlength/2, null, 0, null, null);
lpszm = new byte[nlen+1];
nlen = widechartomultibyte(936, wc_compositecheck,
(const unsigned short*)lpszw, nlength/2, (char*)lpszm, nlen, null, null);
lpszm[nlen] = 0;
cstring cssm((lpctstr)lpszm, nlen);
delete lpszm;
delete lpszw;
return cssm;
}
/*****************************************************************************
* class : global function
* function : to_ucs2
* description: 把多字节转换成unicode
* parameters : char* strsm -- -- 要转换的多字节缓冲
* int nlength -- 多字节缓冲的长度
* return : 转换后的unicode字串
******************************************************************************/
cstring to_ucs2(char* strsm, int nlength){
cstring cssm((lpctstr)strsm, nlength);
pbyte lpszw = null;
uint nlen = 0;
nlen = multibytetowidechar(936, mb_precomposed,
(lpctstr)cssm, cssm.getlength(), null, 0);
lpszw = new byte[nlen * 2];
nlen = multibytetowidechar(936, mb_precomposed,
(lpctstr)cssm, cssm.getlength(), (lpwstr)lpszw, nlen);
for(uint i = 0; i < nlen; i ++)
*((unsigned short*)lpszw + i) = htons(*((unsigned short*)lpszw + i));
cstring csret((lpctstr)lpszw, nlen * 2);
delete lpszw;
return csret;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-125453/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10294527/viewspace-125453/