Multibyte vs widechar Conversion

Source: Internet
Author: 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 matches es 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 abve code section:
// Setlocale (lc_all, ". 65001/. utf8"); // utf8
// Mbstowcs
}

2. My hot Summary of widechartomultibyte () vs. wcstombs () Kind Comparison:

 

1) wcstombs () CILS widechartomultibyte () inside. widechartomultibyte () is the basic function to do the conversion. but because of its too when too detail arguments, CRT provides the instead function to operate 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 and mbstowcs

 

Multibytetowidechar

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

Note:For UTF-8,DwflagsMust 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.