mantian

统计

留言簿(2)

阅读排行榜

评论排行榜

文字列转换(MultiByteToWideChar)

函数如下,使用的时候直接贴在.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版本也弄好了。这样 用起来就更方便了。

posted on 2009-01-21 14:56 岳阳 阅读(3652) 评论(2)  编辑 收藏 引用 所属分类: 『 VC&MFC 』

评论

# re: 文字列转换(MultiByteToWideChar) 2009-01-29 00:49 Nash Tsai

在 WIN32 開發可以用 ATL 裡的

#include <atlconv.h>

ATL::CA2W(str, CP_UTF8) - UTF8 Multibytes to Unicode
ATL::CW2A(wstr, CP_UTF8) - Unicode to UTF8 Multibytes  回复  更多评论   

# re: 文字列转换(MultiByteToWideChar) 2009-01-30 07:27 岳阳

谢谢,记下了。

我是在CE下面,没有找到类似的定义。
不过可以参考一下呵。  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理