Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C++
Tip/Trick

Useful function for conversion between MBCS and WCS

Rate me:
Please Sign up or sign in to vote.
4.67/5 (9 votes)
19 Nov 2010CPOL 21.2K   9   4
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)


Written By
Software Developer
China China
In my first English Lesson, I learned "What's your name?My name is Yubao li",hehe...Here, My name is DotCpp, Which comes from suffix of the c++ file.

I am from beautiful city Chongqing China, famous for "Chongqing hilly" and "Chongqing foggy"

I am interested in c++ programming, but just as a junior.

If you, like me, also come from China, you can visit my Baidu space:
hi.baidu.com/anglecloudy

Comments and Discussions

 
Generalmbstowcs, wcstombs? Pin
alejandro29A29-Dec-10 9:25
alejandro29A29-Dec-10 9:25 
mbstowcs, wcstombs?
GeneralOne of your function calls is using CP_ACP (actual codepage ... Pin
pasztorpisti9-Dec-10 4:34
pasztorpisti9-Dec-10 4:34 
GeneralI have wrote these two help class: class CAnsi { public: C... Pin
qiuchengw2-Dec-10 15:34
qiuchengw2-Dec-10 15:34 
Generala question.. Pin
alejandro29A29-Dec-10 9:26
alejandro29A29-Dec-10 9:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.