UNICODE和ASIC码互换

从ANSIC到UNICODE比较简单
可以用CString的Format函数或默认转换:
char *pAnsicString = "Some test string";
CString strUnicode = pAnsicString;
还有MultiByteToWideChar函数。
从UNICODE到ANSIC也不难:

wcstombs
char* GetAnsicString(const CString &s)
{
int nSize = (s.GetLength() + 1) * sizeof(TCHAR);
char *pAnsicString = new char[nSize];
ZeroMemory(pAnsicString, nSize);
wcstombs(pAnsicString, s, nSize);
return pAnsicString;
}
CString strUnicode = _T("Some test string");
char *pAnsicString = GetAnsicString(strUnicode);


WideCharToMultiByte
WideCharToMultiByte是一个很强大的函数,它有很多参数。它允许你控制很多,象代码页、默认字符集等等。下面是一个例子:
char* GetAnsicString(const CString &s, UINT nCodePage)
{
int nSize = s.GetLength();
char *pAnsicString = new char[(nSize + 1) * sizeof(TCHAR)];
ZeroMemory(pAnsicString, [(nSize + 1) * sizeof(TCHAR));
WideCharToMultiByte(nCodePage, 0, s, nSize, pAnsicString, (nSize + 1) * sizeof(TCHAR), NULL, NULL);
return pAnsicString;
}
CString strUnicode = _T("Some test string");
char *pAnsicString = GetAnsicString(strUnicode, CP_ACP);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值