C ++ character encoding and conversion

Source: Internet
Author: User

// MyTransferTCHAR. cpp: Defines the entry point for the console application.
//

# Include "stdafx. h"
# Include <stdlib. h>

# Include <Windows. h>
# Include <tchar. h> // include <string. h> vs <wchar. h>

/* Part1: width and C */
// The width character is not necessarily UNICODE. UNICODE is only a reflection of the width character encoding.
// In C language, the wide character is based on the wchar_t Data Type
// L "hello", L 'A': indicates that it should be interpreted as A wide character.
// Wchar_t a [] = L "kuanzihello ";
/*
<String. h>:
Size_t _ cdecl strlen (_ In_z _ const char * _ Str)
<Wchar. h>:
Size_t _ cdecl wcslen (_ In_z _ const wchar_t * _ Str)
<Tchar. h>
# Define _ tcslen wcslen/strlen

Typedef wchar_t TCHAR
Typedef char TCHAR
*/

/* Part2: width and Windows */
// Windows. h --> windef. h (winnt. h)
/* # Include <winnt. h>
Typedef char CHAR
Typedef wchar_t TCHAR

*/

/* Part3: String function in Windows */
/* Lstrlen
Lstrcpy
Lstrcpyn
Lstrcat
Lstrcmp
Lstrcmpi */
// Sprintf/swprintf --> _ stprintf
//
// Vsprintf: Execute variable parameters va_list, va_start and va_end (# include <stdarg. h>)

Void test_strlen ()
{
Wchar_t * pw = L "hello! ";
Int iLength = 0;
ILength = wcslen (pw );
}

Void TCHAR2CHAR ()
{
TCHAR src [] = TEXT ("hello! ");
Char dest [256] = {0 };
Int length = 0; // 7
Length = WideCharToMultiByte (CP_ACP, 0, src,-1, NULL, 0, NULL, NULL );
WideCharToMultiByte (CP_ACP, 0, src,-1, dest, length, NULL, NULL );
Printf ("% s \ n", dest );
}

Void CHAR2TCHAR ()
{
CHAR src [] = "hello! ";
TCHAR dest [256] = {0 };
Int length = 0; // 7
Length = MultiByteToWideChar (CP_ACP, 0, src,-1, NULL, 0 );
MultiByteToWideChar (CP_ACP, 0, src,-1, dest, length );
Printf ("% s \ n", dest );
}

Int _ tmain (int argc, _ TCHAR * argv [])
{
CHAR2TCHAR ();
Return 0;
}

 

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.