VC ansic-Unicode Processing

Source: Internet
Author: User

1. ansic and Unicode characters

There is no difference between the two. A single byte and a dual byte, Unicode can represent more characters, suitable for text systems such as Chinese characters.

Define the use of WIDE characters:

2. Methods for declaring Unicode characters and strings:

The _ T () macro must contain tchar. h.

1 wchar_t c = L 'a ';
2 wchar_t szbuffer [10] = l "a string ";
3

Winnt. h defines the macro:

1 # ifdef Unicode
2 typedef wchar tchar, * ptchar, ptstr;
3 typedef const wchar * pctstr;
4 # DEFINE _ text (quote) quote
5 # DEFINE _ text (quote) L # quote
6
7 # else
8 typedef char tchar, * ptchar, ptstr;
9 typedef const char * pctstr;
10 # DEFINE _ text (quote) quote
11
12 # endif
13 # DEFINE _ text (quote) quote text (quote)
14
15

You can use the above macro to define the wide character. When you create a project in Visual Studio, the Unicode option is generally enabled and the wide character can be defined directly.

1 tchar c = txet ('A'); // If Unicode is defined, it is a 16-bit character; otherwise, it is an 8-bit
2 tchar c = l'a ';
3 tchar szbuffer [10] = text ("A string ");
4 tchar szbuffer [10] = _ T ("A string ");
5

3. Conversion between ansic and UNICODE:

1 // define the ansic string
2 char * c = "test! ";
3 STD: cout <C <STD: Endl;
4 // ansic to width
5 Int length = multibytetowidechar (cp_acp, 0, C,-1, null, 0 );
6 wchar_t * pwidecharstr = new wchar_t [sizeof (wchar_t) * length];
7 multibytetowidechar (cp_acp, 0, C, 6, pwidecharstr, length * sizeof (wchar_t ));
8 STD: wcout <"multibytetowidechar:" <pwidecharstr <STD: Endl;
9 // convert wide characters to ansic
10 length = widechartomultibyte (cp_acp, 0, pwidecharstr,-1, null, 0, null, null );
11 char * pmultibyte = new char [length * sizeof (char)];
12 widechartomultibyte (cp_acp, 0, pwidecharstr,-1, pmultibyte, length * sizeof (char), null, null );
13 STD: cout <"widechartomultibyte:" <pmultibyte;
14
15 Delete [] pwidecharstr;
16 Delete [] pmultibyte;

4. Recommended string usage

1). start to think of a text string as a character array, rather than a char or byte array.

2). Use a common data type such as tchar ptstr to represent characters and strings

3). Use the text _ t macro to represent literal characters and strings. Do not mix them.

4). byte and pbyte are used to represent byte, byte pointer, and data buffer.

5) Avoid using printf functions, especially not using % s for mutual conversion between ansic and Unicode.

Multibytetowidechar and widechartomutibyte should be used. For detailed usage, see msdn

6) we recommend that you use Unicode characters from now on for a simple reason. to reuse program code, the COM programming model only supports Unicode.

Neither Microsoft. NET nor new API functions support ansic.

Reprinted from http://dev.21tx.com/2010/08/04/13200.html

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.