Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Useful function for conversion between MBCS and WCS

4.67/5 (9 votes)
19 Nov 2010CPOL 22.4K  
Wrapping WideCharToMultiByte and MultiByteToWideChar
When I program on wince or windows mobile platform, Character encoding constantly bother me. Sometimes, I need disply multibyte character set (not string) on UNICODE platform and Using MultiByteToWideChar can't build a wide string directly. Sometimes, I need save a wide string to file using ANIS without '\0' and Using WideCharToMultiByte can't build a multibyte character set directly, too. So, I write the below code to convert different encoding and different types(set and string) freely.

/******************************************************
*function:  convert multibyte character set to wide-character set
*param:      pwStr--[out] Points to a buffer that receives the translated buffers.
*            pStr--[in] Points to the multibyte character set(or string) to be converted.
*            len --[in] Specify the size in bytes of the string pointed to by the pStr parameter,
*                       or it can be -1 if the string is null terminated.
*            IsEnd--[in]Specify whether you add '\0' to the end of converted array or not.
*return: the length of converted set (or string )
*******************************************************/
int ToWideString( WCHAR* &pwStr, const char* pStr, int len, BOOL IsEnd)
{
    ASSERT_POINTER(pStr, char);
    ASSERT(len >= 0 || len == -1);
    int nWideLen = MultiByteToWideChar(CP_ACP, 0, pStr, len, NULL, 0);
    if (len == -1)
    {
        --nWideLen;
    }
    if (nWideLen == 0)
    {
        return 0;
    }
    if (IsEnd)
    {
        pwStr = new WCHAR[(nWideLen+1)*sizeof(WCHAR)];
        ZeroMemory(pwStr, (nWideLen+1)*sizeof(WCHAR));
    }
    else
    {
        pwStr = new WCHAR[nWideLen*sizeof(WCHAR)];
        ZeroMemory(pwStr, nWideLen*sizeof(WCHAR));
    }
    MultiByteToWideChar(CP_ACP, 0, pStr, len, pwStr, nWideLen);
    return nWideLen;
}
/******************************************************
*function:   convert wide-character  set to multibyte character set
*param:      pStr--[in] Points to a buffer that receives the translated buffer.
*            pwStr--[out] Points to the wide character set ( or string ) to be converted.
*            len --[in] Specify the size in bytes of the string pointed to by the pwStr parameter,
*                       or it can be -1 if the string is null terminated.
*            IsEnd--[in]Specify whether you add '\0' to the end of converted array or not.
*return:     the length of converted set (or string )
*******************************************************/
int ToMultiBytes( char* &pStr, const WCHAR* pwStr, int len, BOOL IsEnd)
{
    ASSERT_POINTER(pwStr, WCHAR) ;
    ASSERT( len >= 0 || len == -1 ) ;
    int nChars = WideCharToMultiByte(CP_ACP, 0, pwStr, len, NULL, 0, NULL, NULL);
    if (len == -1)
    {
        --nChars;
    }
    if (nChars == 0)
    {
        return 0;
    }
    if(IsEnd)
    {
        pStr = new char[nChars+1];
        ZeroMemory(pStr, nChars+1);
    }
    else
    {
        pStr = new char[nChars];
        ZeroMemory(pStr, nChars);
    }
    WideCharToMultiByte(CP_ACP, 0, pwStr, len, pStr, nChars, NULL, NULL);
    return nChars;
}


How to Use
char *pStr = "test";
WCHAR* pwStr;
int nWideLen = ToWideString(pwStr, pStr, -1, TRUE);

WCHAR* pwStr = _T("test");
char *pStr;
int nWideLen = ToMultiBytes(pStr, pwStr, -1, TRUE);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)