函数如下,使用的时候直接贴在.h文件中就可以了。
说明:
iMultiCharSize 默认为-1的时候,null terminated 被计算。
其他情况下需要将转换后的结尾置零。
inline int WINAPI MB2WC(LPCSTR lpcszStr, int iMultiCharSize, LPWSTR lpwszStr, int iBufSize)
{
int iMinSize;
ASSERT(lpcszStr != NULL);
ASSERT(lpwszStr != NULL);
// -1: the string is assumed to be null terminated and the length is calculated automatically.
// If this value is zero, the function returns the required buffer size, in wide characters,
// and makes no use of the lpWideCharStr buffer.
//iMinSize = MultiByteToWideChar(g_uConvCodePage, MB_PRECOMPOSED, lpcszStr, -1, NULL, 0);
iMinSize = MultiByteToWideChar(g_uConvCodePage, MB_PRECOMPOSED, lpcszStr, iMultiCharSize, NULL, 0);
if(iBufSize < iMinSize)
{
return FALSE;
}
// Change the character string to the wide-character (Unicode) string.
MultiByteToWideChar(g_uConvCodePage, MB_PRECOMPOSED, lpcszStr, iMultiCharSize, lpwszStr, iMinSize);
//iMinSize = MultiByteToWideChar(g_uCodePage, MB_PRECOMPOSED, lpcszStr, -1, lpwszStr, iMinSize);
if(iMultiCharSize > 0) // fix 20090121 because when -1 ,the length is calculated
{
lpwszStr[iMinSize] = 0;
}
return iMinSize;
}
使用实例
// +++ test WC2MB
TCHAR szTest[10] = _T("Zhou");
char chBuffer[256];
WC2MB(szTest, 2, chBuffer, 256);
// +++ end test
嘿嘿。
有时间吧Strdup版本也弄好了。这样 用起来就更方便了。