[C + +] Zlatlcv:atl string Conversion Auxiliary library. It is convenient to convert UTF-8 string to TCHAR string

Source: Internet
Author: User
Tags locale

zyl910

Today, UTF-8 strings are used more and more frequently. However, in VC, can not directly deal with the UTF-8 string, you have to specifically write UTF-8 and narrow strings, wide strings, TCHAR strings to convert each other code. Not only is it time-consuming and laborious, but it is easy to create a memory leak with a little attention. So I want to write a library specifically to solve the problem of UTF-8 string encoding.

Characteristics--
Support TCHAR, can switch the project character set configuration at any time.
Compatible with 32-bit (x86) and 64-bit (x64) Windows environments.
Compatible with VC2005 and later versions of VCs.


First, design ideas

string conversion macros in ATL are handy, so I'm going to refer to it and make a string conversion macro.

Naming rules for conversion macros--

C<SOURCETYPE>2[C]<DESTINATIONTYPE>[EX]

<SourceType>, <destinationtype>: String type. Can be a (char), W (wchar_t), T (TCHAR), U8 (UTF-8).
[C]: Whether it is a constant.
[EX]: is not the enhanced version. That is, if you have template parameters such as T_nbufferlength.

For example, common conversion macros have--

CU82A: Converts the UTF-8 string to a narrow string.
Ca2u8: Converts a narrow string to a UTF-8 string.
CU82W: Converts the UTF-8 string to a wide string.
Cw2u8: Converts a wide string to a UTF-8 string.
CU82T: Convert UTF-8 string to TCHAR string.
Ct2u8: Convert Tchar string to UTF-8 string.


Second, sample code

Sample code--

#include <stdio.h>#include<locale.h>#include<tchar.h>#include"zlatlcv.h"//"Welcome": 中文版, Traditional Chinese, Japanese, Korean.Const Char* PSA ="a_welcome_ Welcome to _ようこそ_??.";//!< UTF-8 string (Auto. File used UTF-8 encoding).Constwchar_t* PSW = L"w_welcome_\u6b61\u8fce_\u3088\u3046\u3053\u305d_\ud658\uc601.";//!< Wide char string.int_tmain (intARGC, _tchar*argv[]) {    //init.SetLocale (Lc_all,"");//Use the default locale for the customer environment. //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 turns 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 A:\t%s\n"), PSAT); _tprintf (_t ("_tprintf W:\t%s\n"), PSWT); _tprintf (_t ("_tprintf u8:\t%s\n"), psu8t); return 0;}


Operating Effect--

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

[C + +] Zlatlcv:atl string Conversion Auxiliary library. It is convenient to convert UTF-8 string to TCHAR string

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.