[C ++] zlatlcv: Auxiliary library for ATL String Conversion. Can easily convert the UTF-8 string to a string such as TCHAR, zlatlcvtchar

Source: Internet
Author: User

[C ++] zlatlcv: Auxiliary library for ATL String Conversion. Can easily convert the UTF-8 string to a string such as TCHAR, zlatlcvtchar

Author: zyl910

 

Today, UTF-8 strings are increasingly used. But in VC, you cannot directly process the UTF-8 string, you have to specifically write UTF-8 and narrow string, wide string, TCHAR String Conversion code. It is not only time-consuming and laborious, but it is easy to cause memory leakage if you do not pay attention to it. So I want to write a library to solve the problem of UTF-8 string encoding.

Feature --
TCHAR is supported and the character set configuration of the project can be switched at any time.
Compatible with 32-bit (x86) and 64-bit (x64) Windows environments.
Compatible with VC2005 and later versions of VC.


I. Design Ideas

The String Conversion macro in ATL is very convenient to use, so I plan to refer to it and make a set of String Conversion macros.

Convert macro naming rules --

C <SourceType> 2 [C] <DestinationType> [EX]

<SourceType> and <DestinationType>: string type. It can be A (char), W (wchar_t), T (TCHAR), U8 (UTF-8 ).
[C]: whether it is a constant.
[EX]: whether it is an enhanced version. Whether a template parameter such as t_nBufferLength exists.

For example, commonly used conversion macros include --

CU82A: converts a UTF-8 string into a narrow string.
CA2U8: converts a narrow string into a UTF-8 string.
CU82W: converts a UTF-8 string to a wide string.
CW2U8: converts a wide string into a UTF-8 string.
CU82T: Convert the UTF-8 string to a TCHAR string.
CT2U8: convert a TCHAR string to a UTF-8 string.


Ii. Sample Code

Sample Code --

# Include <stdio. h> # include <locale. h> # include <tchar. h> # include "zlatlcv. h "//" Welcome ": English, Traditional Chinese, Japanese, Korean. const char * psa = "A_Welcome _ うこそ _ 환 영. ";//! <UTF-8 string (Auto. file used UTF-8 encoding ). const wchar_t * psw = L "W_Welcome _ \ u6B61 \ u8FCE _ \ u3088 \ u3046 \ u3053 \ u305D _ \ uD658 \ uC601 .";//! <Wide char string.int _ tmain (int argc, _ TCHAR * argv []) {// init. setlocale (LC_ALL, ""); // use the default locale. // title. _ tprintf (_ T ("zlatlcv v1.0 (% dbit) \ n"), (int) (8 * sizeof (int *))); _ tprintf (_ T ("sizeof (wchar_t): % d \ n"), (int) (sizeof (wchar_t ))); _ tprintf (_ T ("sizeof (TCHAR): % d \ n"), (int) (sizeof (TCHAR ))); _ tprintf (_ T ("\ n"); // printf. fflush (stdout); printf ("printf A: \ t % s \ n", psa); printf ("printf W: \ t % ls \ n", psw ); printf ("\ n"); // UTF-8 to string (UTF-8 to various strings ). // CA2AZ psaa (psa, CP_UTF8, 0); CU82A psaa (psa); CU82W psaw (psa); printf ("printf A from UTF-8: \ t % s \ n ", psaa); printf ("printf W from UTF-8: \ t % ls \ n", psaw); printf ("\ n "); // string to UTF-8 (various string to UTF-8 ). CA2U8 psau8 (psaa); CW2U8 pswu8 (psaw); fflush (stdout); printf ("printf UTF-8 from A: \ t % s \ n", psau8 ); printf ("printf UTF-8 from W: \ t % s \ n", pswu8); // _ tprintf. CA2CT psat (psa); CW2CT pswt (psw); CU82T psu8t (psa); fflush (stdout); _ tprintf (_ T ("_ tprintf: \ t % s \ n "), psat); _ tprintf (_ T (" _ tprintf W: \ t % s \ n "), pswt ); _ tprintf (_ T ("_ tprintf U8: \ t % s \ n"), psu8t); return 0 ;}

 


Running Effect --

 

 

Download source code --
Https://github.com/zyl910/zlatlcv

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.