Multibyte VS WideChar Conversion

來源:互聯網
上載者:User

1. Multibyte shows to us as char*. While in fact, it can be any code page encoding, including gbk, utf8, etc. If a char* represents utf8 characters, we need to handle it specially in below way:

// Convert unicode(windows default utf16) to utf8 char* string.

MakeUTF8String(const wchar_t* pWide)

{
    // Compute the required size of the buffer by passing cbMultiByte as 0
    MCAD_ASSERT(pWide);
    int nBytes = ::WideCharToMultiByte(CP_UTF8, 0, pWide, -1, NULL, 0, NULL, NULL);
    MCAD_ASSERT(nBytes > 0);

    char* pNarrow = new char[nBytes + 1];
    nBytes = ::WideCharToMultiByte(CP_UTF8, 0, pWide, -1, pNarrow, nBytes + 1, NULL, NULL);
    UTxString8 utf8Str(pNarrow);
    MCAD_ASSERT(nBytes);
    delete [] pNarrow;
    return utf8Str;

}

 

// Convert a utf8 char* to be utf16 unicode string wchar*.

MakeWideString(const char* pNarrow)
{
     // Get the required size of the buffer that receives the Unicode string.
    DWORD dwMinSize;
    dwMinSize = MultiByteToWideChar (CP_UTF8, 0, pNarrow, -1, NULL, 0);

    // Convert headers from ASCII to Unicode.
    wchar* pWide = new wchar[nBytes + 1];
    MultiByteToWideChar (CP_UTF8, 0, pNarrow, -1, pWide, dwMinSize);

    // Below method can work as      above code section:
    // setlocale(LC_ALL, ".65001/.UTF8"); // utf8
    // mbstowcs     
}

 

2. My hot summary of WideCharToMultiByte() vs. wcstombs() kind comparison:

 

1) wcstombs() calls WideCharToMultiByte() inside.  WideCharToMultiByte() is the basic function to do the conversion. But because of its too many too detail arguments, CRT provides the instead function to ease our work. Very good consideration!

 

2) When call WideCharToMultiByte(), there is an argument by which we can set the code page to be used to do the conversion. While for wcstombs(), it just use current system locale. So in order to use these 2 functions really in the same place, Setlocale must be called before wcstombs().

 

 

Resource related:

wcstombs和mbstowcs

 

MultiByteToWideChar

http://msdn.microsoft.com/en-us/library/ms776413(VS.85).aspx

Note: For UTF-8, dwFlags must be set to either 0 or MB_ERR_INVALID_CHARS. Otherwise, the function fails with ERROR_INVALID_FLAGS.

 

mbstowcs

http://msdn.microsoft.com/en-us/library/k1f9b8cy(VS.71).aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.