Wchar_t and char

Source: Internet
Author: User

Example:

Int n_wchar_t = sizeof (wchar_t); // n_wchar_t = 2

Int n_char = sizeof (char); // n_char = 1

 

The preceding example shows that wchar_t and Char have different lengths.

 

Example:

Wchar_t * wch = l "abcdef ";

Int n_wch = wcslen (wch); // n_wch = 6

Char * Ch = "abcdef ";

Int n_ch = strlen (CH); // n_ch = 6

 

As shown in the preceding example, wcslen and strlen have the same functions. Others only have different prefixes and have the same functions.

 

 

Example:

 

Char * Ch = "abcdef ";

Wchar_t * wch;

 

Wch = (wchar_t *) ch;

Int n_ch = strlen (CH); // n_ch = 6

Int n_wch = wcslen (wch); // n_wch = 3

 

As shown in the preceding example, when the char pointer variable is forcibly converted to the wchar_t pointer variable, two characters in the memory unit pointed to by the char pointer will be treated as one character and unexpected results will appear, you can use breakpoints for debugging.

 

 

Example:

Char CH = 'C ';

Wchar_t wch;

Wch = CH; // wch = 'C'

 

The preceding example shows that there is no error when there is only one character.

 

 

Example of converting char * To wchar_t *

 

Char * src = "adscd ";

Int dest_len;

 

Int J = strlen (SRC); // J = 5

 

Dest_len = multibytetowidechar (cp_acp, 0, SRC,-1, null, 0); // get the length required for the conversion wchar_t * variable dest_len = 6

Wchar_t * DEST = new wchar_t [dest_len]; // DEST = l "adscd"

 

ID (DEST = NULL)

Return;

 

Int I = wcslen (DEST); // I = 5

 

If (DEST)

{

Delete [] DEST;

DeST = NULL;

}

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.