본문 바로가기
VisualStudio.C++.C#/코딩팁,함수활용,단편

VC++. CW2A, CA2W 멀티바이트 <-> 유니코드 변환 클래스.

by i.got.it 2015. 12. 18.

 

 

CA2W, CW2A

 

현재시점(2015년 12월) VC++ 에서 멀티바이트 문자 <-> 유니코드 문자 변환에 사용하기 가장 간편한 것.

 

기본 사용예.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Example 1
// Convert LPCWSTR to LPCSTR.
void ExampleFunction1(LPCWSTR pszW)
{
   // Create an instance of CW2A, called pszA,
   // and initialize it with pszW.
   CW2A pszA(pszW);
   // pszA works like an LPCSTR, and can be used thus:
   ExampleFunctionA(pszA);  
   // Note: pszA will become invalid when it goes out of scope.
}
 
// Example 2
// Use a temporary instance of CW2A.
void ExampleFunction2(LPCWSTR pszW)
{
   // Create a temporary instance of CW2A,
   // and initialize it with pszW.
   ExampleFunctionA(CW2A(pszW));
   // Note: the temporary instance becomes invalid 
   // after the execution of the statement above.
}
 
// Example 3
// Incorrect use of conversion macros.
void ExampleFunction3(LPCWSTR pszW)
{
   // Create a temporary instance of CW2A,
   // save a pointer to it and then delete
   // the temportary instance.
   LPCSTR pszA = CW2A(pszW);
   // The pszA in the following line is an invalid pointer,
   // as the instance of CW2A has gone out of scope.
   ExampleFunctionA(pszA);
}
 
 

 

버퍼사이즈 변경 방법.         

기본 정적 버퍼 크기는 128자이나, 

버퍼 크기를 변경해야 하는 경우 매크로의 EX 버전을 사용하고 템플릿 인수로 버퍼 크기를 지정가능.

// Example 4
// Changing the size of the buffer.
void ExampleFunction4(LPCWSTR pszW)
{
   // Use a 16-character buffer.
   ExampleFunctionA(CW2AEX<16>(pszW));
}


아래에는 클래스의 생성자에 대해 코드 페이지를 두 번째 매개 변수로 지정하는 예.

C++

// Example 5 // Specifying the code page. void ExampleFunction5(LPCWSTR pszW) { // Convert to the Macintosh code page ExampleFunctionA(CW2A(pszW, CP_MACCP)); }


from MSDN. 

 

 

 

 본 글이 포함된 상위 정리 장소.

 

 Visual Studio/VC++/C/C# 활용정리 -> http://igotit.tistory.com/11

 

 

 ///501.

댓글



 

비트코인




암호화폐       외환/나스닥/골드       암호화폐/외환/나스닥/골드 암호화폐/외환/나스닥/골드   암호화폐/외환/나스닥/골드
     
현물 |선물 인버스 |선물 USDT       전략매니저(카피트레이딩)     롤오버 이자 없는 스왑프리계좌
( 스왑프리 암호화폐도 거래 가능 )    
MT4, MT5 , cTrader 모두 지원     FTMO 계좌 매매운용. MT4,MT5