この記事では、コード例とともに C++ の TCHAR、WCHAR、LPSTR、LPWSTR、LPCTSTR について学習します。
多くの場合、文字を表すために 1 バイトまたは 2 バイトを使用できます。
サイズが 1 バイトの ANSI 標準は、すべての英語の文字を表すために使用されます。
世界のすべての言語は、サイズが 2 バイトの Unicode で表されます。
Visual C++ コンパイラはサポートしています
ANSI および Unicode 文字用の TCHAR、WCHAR、LPSTR、LPWSTR、LPCTSTR。
TCHAR
TCHAR は、ANSI または Unicode 文字列を記述するために使用されます。 char の型は頭字語 TCHAR で参照されます。すべてのプログラミング言語は、Unicode でのコーディングをサポートしています。 TCHAR は ASCII またはマルチバイト文字の char を表し、1 バイトを割り当てますが、Unicode では WCHAR を示し、2 バイトを割り当てます。
構文
Typedef char TCHAR;
通常は次のように定義します:
#ifndef _UNICODE
typedef char TCHAR;
#else
typedef wchar_t TCHAR;
#endif
WCHAR
16 ビットの Unicode 文字を含む Windows データ型。頭字語 WCHAR で参照されます。 wchar_t タイプとも呼ばれます。
構文
Typedef char TCHAR;
通常は次のように定義されます:
#if !defined(_NATIVE_WCHAR_T_DEFINED)
typedef unsigned short WCHAR;
#else
typedef wchar_t WCHAR;
#endif
プログラム コード
//wchar implementation in cpp
#include <iostream>
#include<cwchar>
using namespace std;
//main code
int main()
{
wchar_t string1[] = L"Hello" ;
wchar_t string2[] = L"world" ;
//concat stirng
wcscat(string1, string2);
//display string
wcout << L"STRING = "
<< string1 << endl;
return 0;
}
出力
STRING : Hello world
LPSTR
LPSTR は長いポインタ文字列です。 char * または wchar_t * uopn に依存
uncicod が定義されているかどうか。
どこ
LPはロングポインターの略。
STR はストリングの略です。
LPSTR は、NULL で終わる CHAR またはロング ポインター定数の定数文字列を意味します。
構文
typedef const char* LPSTR;
プログラム コード
#include<iostream>
#include<Windows.h>
using namespace std;
main()
{
string str1 = "Welcome";
LPSTR str2 = new TCHAR[str1.size() + 1];
//copy string
strcpy(str2, str1.c_str());
cout << "String 1 : " << str1 <<endl;
cout << "Copy String : " << str2 <<endl;
}
出力
String 1 : Welcome
Copy String : Welcome
LPWSTR
LPWSTR は、16 ビットの Unicode 文字の文字列への 32 ビット ポインターです。
null 文字で null 終端することができます。
つまり、WCHAR 型のヌル終了文字列またはロング ポインター wchar 型の文字列です。
構文
typedef const char* LPWSTR;
#include <string>
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
LPWSTR str1 = L"Hello ";
LPWSTR str2 = L"World";
wstring w1(str1);
wstring w2(str2);
wstring w3 = w1+w2;
wcout << w3 << endl;
}
出力
Hello World
LPCSTR
LPCSTR は、8 ビット Windows (ANSI) 文字の定数 null 終了文字列への 32 ビット ポインターです。つまり、単純な文字列です。これは Microsoft によって定義されています。このデータ型を宣言するには、windows.h ヘッダー ファイルをプログラムに含める必要があります。
どこ
LPはロングポインターの略。
C 定数の略
STR は文字列の略
構文
typedef const char* LPCSTR;
LPCSTR は、NULL で終わる CHAR の定数文字列またはロング ポインター定数を意味します。
プログラム コード
#include<iostream>
#include<Windows.h>
using namespace std;
main()
{
wstring str1 = L"Hello World";
LPCSTR str2 ;
//copy string
str2 = str1.c_str();
wcout << "String 1 : " << str1 <<endl;
wcout << "String 2 : " << str2 <<endl;
}
出力
String 1 : Hello World
String 2 : Hello World