Introduction to C + + wchar_t and conversion to string type

Source: Internet
Author: User

First introduce the next wchar_t type

First, the origin of the wchar_t type

We know that a char type variable can store a byte character, it can be used to save English characters and punctuation, but not for characters such as kanji, Korean, and Japanese, because Chinese, Korean, and Japanese each have two bytes, in order to solve this problem, C + + puts forward WCHAR The _t type, called a double-byte type, is also known as a wide character type.

Second, the following is an example

int main (int argc, wchar_t* argv[]) {//Use the setlocale function to set the native language to Chinese Simplified setlocale (lc_all, "CHS"),//lc_all to set all options (including financial currency, decimal point, Time date format, language string usage habits, etc.), CHS Express Chinese Simplified wchar_t wt[] = L "Hello China!" ";//capital Letter L tells the compiler to allocate two bytes of space for" medium "wcout<<wt<<endl;//use wcout instead of cout output wide character, Wcin class instead of CIN input wide character cout<< wcslen (WT) <<endl;//wcslen the length of the output wide string, the output length is 5cout<<sizeof (WT) <<endl;//the output length is 12 bytes, the last wchar_t type of ' \ 0 ' two bytes return 0;}
Conversion between three, and string types

String str ("Hello China"); wchar_t * WC = new Wchar_t[str.size ()];swprintf (wc,100,l "%s", Str.c_str ()); Note the uppercase//WC points to the memory area to store this wchar_t type of "Hello China".



Introduction to C + + wchar_t and conversion to string type

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.