Unicode data type (with ANSI relationships and mutual conversion)

Source: Internet
Author: User

1. Windows defines Unicode Data Types
Wchar (UNICODE character)
Pwstr (pointer to Unicode string)
Pcwstr (pointer to a constant Unicode string)
The above are Unicode characters and strings. At the same time, Windows defines the ANSI/Unicode generic data types ptstr and pctstr.
Unicode macros are not prefixed with underscores. _ Unicode macros are used for the C Runtime header files, while Unicode is used for the Windows header files.

2. c supports Unicode
For example, common functions: char * strchr (const char *, INT) ---------> wchar_t * wcschr (const wchar_t *, wchar_t );
Int strcmp (const char *, const char *) --------> int wcscmp (const wchar_t *, const wchar_t *);
For example, strcpy ---> wcscpy, strlen-> wcslen; instead of ANSI prefix STR.
3, tchar. h
The tchar. h file is used only to create ANSI and Unicode universal source code.
If _ Unicode is defined, tchar declares:
Typedef wchar_t tchar
If _ UNICODE:
Typedef char tchar;
Assign a tchar szstring [100]; or tchar * Sze = "Hello World ";
If Unicode is not an ANSI string, it must be rewritten to tchar * Sze = l "Hello World"; (L indicates Unicode string compilation );
You can also rewrite it to tchar * Sze = _ text ("Hello World ");
4. We recommend that you use system functions in Windows programming, instead of running string functions in C.
Add the shlapi. h header file. Functions look like strcat and strchr (uppercase letters at the beginning)
5. Windows string functions
Unicode functions provided by windows, for example:
Lstrcat, lstcmp, lstcmpi, lstcpy, and lstrlen. These functions can call Unicode or ANSI.
If Unicode is not defined, lstcat is extended to lstcata. If Unicode is defined, it is extended to strcatw.

6. Unicode and ANSI Conversion
Char sza [100]; // an ANSI String Buffer
Wchar SZW [100]; // a unicode String Buffer
Sprintf (sza, "% s", "ANSI Str"); // normal sprintf: All string are ANSI
Sprintf (sza, "% s", l "Unicode Str"); // converts Unicode string to ANSI
Swprintf (SZW, l "% s", l "Unicode Str"); // normal swprintf: All string are Unicode
Swprintf (SZW, l "% s", "ANSI Str"); // converts ANSI string to Unicode
(Case Sensitive S)
Lptstr is equivalent to char *
Lpcstr is equivalent to const char *
7. Conversion between Unicode and ANSI (function reference msdn)
Multibytetowidechar (convert a multi-byte string to a wide byte string)
Widechartomultibye (convert a wide string to a multi-byte string)

Instance:

Char Buf [max_path];
Size_t Alen = strlen (BUF) + 1;

Int WLEN = multibytetowidechar (cp_acp, 0, Buf, Alen, null, 0 );

Lpwstr lpw = new wchar [WLEN];
Multibytetowidechar (cp_acp, 0, Buf, Alen, lpw, WLEN );
............ // Use lpw
Delete [] lpw;

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ameyume/archive/2009/08/16/4448398.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.