천객만래 [千客萬來] (It has an interminable succession of visitors)

LPSTR : char *

LPCSTR : const char *

LPCTSTR : const char * 또는 const WCHAR *



WORD : 보통 2바이트의 부호없는 정수형   (*.word형을 string형으로 바꾸기 string := IntToStr(word); )

DWORD : 4바이트의 부호없는 정수형    DWORD unsigned long

그럼, WORD와, int의 차이점은...

WORD는 4글자 int는 3글자
typedef unsigned short         WORD;    //WORD는 예약어가 아니죠  (typedef는 변수부분)

                                                                                               (cf.define은 전처리 부분)


*.단순참고

char, unsigned char, signed char : 1 byte
short, unsigned short : 2 bytes
int, unsigned int : 4 bytes
long, unsigned long : 4 bytes
float : 4 bytes
double : 8 bytes
long double : 10 bytes

=================================================================================



1. C 자료형
   char(1), short(2), int(4), long(4), float(4), double(8), bool
   문자: char



 
2. Win32 API 자료형
   BYTE(1, unsigned char), WORD(2, unsigned short), UINT(4, unsigned int)
   DWORD(4, unsigned long), LONG(4,long), BOOL
   문자: UCHAR(unsigned char)
   Handle: 대상을 구분하는 4바이트 정수(HWND,HDC...)
  
   MBCS문자(열)  유니코드문자(열)  자동매크로문자(열)
   ------------         -----------------        ------------------
   char   wchar_t    TCHAR
   LPSTR(char*)         LPWSTR(wchar_t*)         LPTSTR
   LPCSTR(const char*)  LPCWSTR(const wchar_t *) LPCTSTR
 
   .LPTSTR과 LPCTSTR를 사용하는 것이 좋음.
   .OLECHAR(wchar_t), LPOLESTR(LPWSTR), LPCOLESTR(LPCWSTR), OLESTR(x) = _T(x)




3. COM 스트링
   BSTR : 문자열 길이를 시작전에 저장하고, 이어서 유티코드문자열을 저장하는 방식
   LPCWSTR -> BSTR : 생성안됨. 생성함수를 이용해야 함.
                     BSTR bstr = sysAllocString(L"HELLO HI"); // 메모리 할당
                                 sysFreeString(bstr);         // 메모리 제거



   VARIANT: 문자열이 들어올때 BSTR과 동일

VARIANT 자료형은 각 개발 환경의 자료형의 차이를 해결하기위하여 제공되는 공용체 이다.


예제) INT nIndex 와 CString strItem이 있다고 가정하고...


// VARIANT 형 선언

VARIANT varIndex, varItem;


// VARIANT type 지정
varIndex.vt = VT_INT;
varItem.vt = VT_BSTR;


// VARIANT 값 할당.
varIndex.intVal = nIndex;
varItem.bstrVal = strItem.AllocSysString();// CString을 bstr로 변환하는 함수


// VARIANT 인자를 사용하여 함수 호출




 

4. CRT(c runtime library)지원 스트링 클래스 (#include "comdef.h")
   4-1. _bstr_t
        :BSTR 랩퍼 클래스, 메모리 할당/제거를 자동으로 수행
      
        . LPCSTR, LPCWSTR  -> _bstr_t
          :_bstr bstr = "hello hi";
        . _bstr_t -> LPCSTR, LPCWSTR
          : LPCSTR psz1 = (LPCSTR)bs1;
        . _bstr_t -> BSTR
          : 형변환 안됨. 함수이용
            BSTR bstr = bs1.copy();
            sysFreeString(bstr); // BSTR은 사용후 메모리 해제를 해야함.

   4-2. _variant_t
        :VARIANT 랩퍼 클래스, 메모리 할당/제거 자동 수행
       
        . LPCSTR, LPCWSTR -> _variant_t
          : _variant_t v1 = "hello hi";
        . _variant_t -> _bstr_t -> LPCSTR,LPCWSTR
          : LPCSTR psz1 = (LPCSTR)(_bstr_t)v1;




5. ATL 지원 스트링클래스
   5-1 CComBSTR : BSTR랩퍼클래스, 메모리할당/제거 자동 수행
      . LPCSTR, LPCWSTR -> CComBSTR
        CComBSTR bs1 = "hello hi";
      . CComBSTR -> BSTR -> LPCWSTR
        BSTR bs = (BSTR)bs1;
      . BSTR -> CComBSTR
        CComBSTR bs2; bs2.Attach(W2BSTR(L"hello hi");

   5-2 CComVariant: VARIANT랩퍼클래스, 메모리할당/제거 자동 수행
      . LPCSTR, LPCWSTR -> CComVariant
        CComVariant bs1 = "hello hi";
      . CComVariant -> CComBSTR -> BSTR -> LPCWSTR
        CComBSTR bs = bs1.bstrVal;



6. STL 스트링
   6-1 string
       . LPCSTR -> string
         string str = "hello hi";
       . string -> LPCSTR (형변환 안됨. 함수 이용)
         LPCSTR psz = str.c_str();
   6-2 wstring
       . LPCWSTR -> wstring
         wstring str = "hello hi";
       . wstring -> LPCWSTR
         LPCWSTR psz = str.c_str();



7. MFC 스트링
   . LPCSTR, LPCWSTR -> CString
     CString str = "hello hi";
   . CString -> LPCTSTR
     1. LPCTSTR lpsz = (LPCTSTR)str;
     2. LPTSTR lptsz = str.getBuffer(0), str.ReleaseBuffer(); (올바른 사용)
     3. LPTSTR lptsz = (LPTSTR)(LPCTSTR)str; (잘못된 표현)
     4. CString -> BSTR
        BSTR bstr = str.AllocSysString(); sysFreeString(bstr);



8. VC7 스트링
   String: .Net에서 새로 정의한 스트링 클래스
          String* s1 = S"hello hi";
          CString s2(s1);
    

9. ETC

   1. BSTR --> LPCSTR
      USES_CONVERSION;
      LPCSTR lpaszTemp = OLE2CA(bstrValue);

   2. LPCSTR --> BSTR
      USES_CONVERSION;
      BSTR bstrTemp = ::SysAllocString(A2COLE(lpaszValue));

   3. CString --> LPCSTR
      1) ANSI 버전
          LPCSTR lpaszTemp = (LPCSTR) strValue;
      2) UNICODE 버전
          USES_CONVERSION;
          LPCSTR lpaszTemp = T2CA((LPCTSTR) strValue);

   4. LPCSTR --> CString
      1) ANSI 버전
         CString strTemp = lpaszValue;
      2) UNICODE 버전
          USES_CONVERSION;
          CString strTemp = A2CT(lpaszValue);

Posted by SB패밀리


CString -> char *

CString str;
str = "Hello";
char* ss = LPSTR(LPCTSTR(str));

 

char * --> CString

char ss[] = "Hello";
CString str;
str.Format("%s", ss);

(Format대신에 GetBuffer()를 써도 됩니다.)
 
지식인!
 
Environment: Compiled using VC6.0 Sp3 and tested using Win95/98 WinNT4.0 and Win 2000
Here are a few data Conversions with small examples :-

PART ONE :- DECIMAL CONVERSIONS
Decimal To Hexa :-
Use _itoa( ) function and set radix to 16.

 

char hexstring[10];
int number = 30;
itoa( number, hexstring, 16);

In hexstring is 1e.

Hexa To Decimal :-
a)You can use strtol function and you can specify base.

char * hexstring= "ABCDEF";
char * p;
int number = strtol(hexstring, &p,16);

b) Or you can use this too

bool HexToDecimal (char* HexNumber, int& Number)
{
char* pStopString;
Number = strtol (HexNumber, &pStopString, 16);
return (bool)(Number != LONG_MAX);
}


Decimal to Time :-

char *DecToTime(float fTime, char *szTime)
{
int nHrs, nMin, nSec;
fTime *= 3600;
nHrs = (int)fTime / 3600;
nMin = (int)(fTime - nHrs * 3600) / 60;
nSec = (int)(fTime - nHrs * 3600 - nMin * 60);
wsprintf(szTime, "%02d.%02d.%02d Hrs.Min.Sec.", nHrs, nMin, nSec);
return szTime;
}

PART TWO :- STRING CONVERSIONS
String to Hexa :-

sscanf(string, %04X, &your_word16);
// where string = your string and 04 = length of your string and X = hex


Hex to CString :-
CString Str;
unsigned char Write_Buff[1];
Write_Buff[0] = 0x01;
Str.Format("0x0%x",Write_Buff[0]);


COleVariant to CString :-
CString strTemp;
COleVariant Var;
Var = "FirstName";
strTemp = Var.bstrVal;
AfxMessageBox(strTemp);


CString to Char Pointer :-
a) CString MyString = "ABCDEF";
char * szMyString = (char *) (LPCTSTR) MyString;

b) char *pBuffer = new char[1024];
CString strBuf = "Test";
pBuffer = strBuf.GetBuffer(sizeof(pBuffer));

 

Char Pointer to CString :-
char * mystring = "12345";
CString string = mystring;


Double to CString including the fractional part :-
CString strValue,strInt, strDecimal;
int decimal,sign;
double dValue = 4.125;
strValue = _fcvt(dValue,6,&decimal,&sign);

// Now decimal contains 1 because there is only one digit before the .

strInt = strValue.Left(decimal); // strInt contains 4
strDecimal = strValue.Mid(decimal); // strDecimal contains 125

CString strFinalVal;
strFinalVal.Format("%s.%s",strInt,strDecimal); // strFinalVal contains 4.125

 

Double To CString :-
CString strValue;
int decimal,sign;

double dValue = 123456789101112;
strValue = _ecvt(dValue,15,&decimal,&sign);


CString To Double :-
strValue = "121110987654321";
dValue = atof(strValue);


CString to LPCSTR :-
CString str1 = _T("My String");
int nLen = str1.GetLength();
LPCSTR lpszBuf = str1.GetBuffer(nLen);
// here do something with lpszBuf...........
str1.ReleaseBuffer();


CString to LPSTR :-
CString str = _T("My String");
int nLen = str.GetLength();
LPTSTR lpszBuf = str.GetBuffer(nLen);
// here do something with lpszBuf...........
str.ReleaseBuffer();

CString to WCHAR* :-
CString str = "A string here" ;
LPWSTR lpszW = new WCHAR[255];

LPTSTR lpStr = str.GetBuffer( str.GetLength() );
int nLen = MultiByteToWideChar(CP_ACP, 0,lpStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW, nLen);
AFunctionUsesWCHAR( lpszW );
delete[] lpszW;

 

LPTSTR to LPWSTR :-
int nLen = MultiByteToWideChar(CP_ACP, 0, lptStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lptStr, -1, lpwStr, nLen);


string to BSTR
string ss = "Girish";
BSTR _bstr_home = A2BSTR(ss.c_str());


CString to BSTR :-
CString str = "whatever" ;
BSTR resultsString = str.AllocSysString();


_bstr_t to CString :-

#include
#include
_bstr_t bsText("Hai Bayram");
CString strName;
W2A(bsText, strName.GetBuffer(256), 256);
strName.ReleaseBuffer();
AfxMessageBox(strName);

char szFileName[256];
GetModuleFileName(NULL,szFileName,256);
AfxMessageBox(szFileName);


PART THREE :- CHARACTER ARRAYS

Char array to integer
char MyArray[20];
int nValue;

nValue = atoi(MyArray);

 

Char array to float
char MyArray[20];
float fValue;

fValue = atof(MyArray);


Char Pointer to double :-
char *str = " -343.23 ";
double dVal;
dVal = atof( str );


Char Pointer to integer :-
char *str = " -343.23 ";
int iVal;
iVal = atoi( str );

 

Char Pointer to long :-
char *str = "99999";
long lVal;
lVal = atol( str );


Char* to BSTR :-
char * p = "whatever";
_bstr_t bstr = p;

Float to WORD and Vice Versa :-
float fVar;
WORD wVar;
fVar = 247.346;
wVar = (WORD)fVar; //Converting from float to WORD. The value in wVar would be 247
wVar = 247;
fVar = (float)fVar; //Converting from WORD to float. The value in fVar would be 247.00

# cstring to int

int 형 = _ttoi(cstring 형)


# int to cstring

cstring 형.Format( _T("%d"), int 형); 
 
* CString -> std::string

 

CString cs ("Hello");

  // Convert a TCHAR string to a LPCSTR
  CT2CA pszConvertedAnsiString (cs);

  // construct a std::string using the LPCSTR input
  std::string strStd (pszConvertedAnsiString);

 

 

* std::string -> CString

 

CString tmp;

string foo;

tmp = foo.c_str();

Posted by SB패밀리