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

'LPTSTR'에 해당되는 글 2건

  1. 2011.12.20 [개발/MFC] 형 변환
  2. 2010.09.30 [VC++/MFC] CString to char * 와 char * to CString
[개발/MFC] 형 변환

http://blog.naver.com/PostView.nhn?blogId=wlsdnrtjd&logNo=20141848341&categoryNo=0&parentCategoryNo=17&viewDate=&currentPage=6&postListTopCurrentPage=&isAfterWrite=true&userTopListOpen=true&userTopListCount=10&userTopListManageOpen=false&userTopListCurrentPage=6

▶ CString -> BYTE
BYTE*   temp;
CString   cmd;
에서 cmd 의 값을 temp에 할당하려 할때.
 
temp=new BYTE[255];
temp=(LPBYTE)(LPCSTR)cmd;
delete []temp;
 
or
 
CString str = _T("abcd");
BYTE* pbyte = new BYTE[256];
int nSize;
nSize = str.GetLength();
CopyMemory( pbyte, str.GetBuffer(nSize), nSize );
pbyte[nSize] = 0;
 
or
 
strcpy(szNamePlace,(LPCTSTR)name);
or
 
CString str = "string";
BYTE* pByte;
pByte = (BYTE*)(LPTSTR)(LPCTSTR)str;

▶ BYTE -> CString
CString testString;
BYTE    testByte;
testString.Format( "%s", testByte );
 
▶ CString -> BYTE *
CString name = "몽룡이";
 BYTE byte[26] = {0};
 BYTE bName[26] = {0x0,};
 sprintf((char*)byte, "%s", name);
 
 memcpy(bName, byte, 26);
 
CString strTmp1, strTmp2;
 strTmp1 = "";
 strTmp2 = "";
 for(int i=0; i<26; i++) {
     strTmp1.Format("%02X ", bName[i]);
     strTmp2 += strTmp1;
 }
 MessageBox(strTmp2, "", 0);
 
26바이트의 크기의 이름이다. 남는 공간은 0으로 채워진다
 
▶  CString -> int

 CString의 문자열을 바로 숫자로 바꾸는것은 
 아직 보지 못했습니다.
 아마 atoi()나 atod()의 C함수를 사용해야 될것 같네요.
 도움말을 참고하세요.


▶  int -> CString

 CString str;
 int i = 6;
 str.Format("%d",i);    // str에 6의 문자가 들어갑니다.


▶  BYTE -> int, int -> BYTE 
 
 바로 형변환으로 가능합니다.
 
 bt = (BYTE)i;          // 주의 : 작은 크기로 들어가기 때문에 
                        // 255 이상의 값은 엉뚱하게 동작하겠지요.
 i = (int)bt;
 
▶ CString  => char* 변환

char * ch;
CString *str;

1) ch = (LPSTR)(LPCSTR)str;
2) ch = str.GetBuffer(str.GetLength());
3) wsprintf( ch, "%s", str);
 

▶ char*  =>  CString 변환

1) str = (LPCSTR)(LPSTR)ch;
2) str = ch;
 
 
참고)
LPSTR 은 char* 입니다.
LPSTR : char stirng의 32비트 포인터, char* 와 같다.
LPCTSTR : Constant character String의 32비트 포인터
UINT : 32비트 unsigned형 정수
DWORD : unsigned long int형
BYTE : 8비트 unsigned 정수
 
 
1.CString 클래스의 GetBuffer()는 CString을 char *로 바꿔줍니다. 

ex) CString strTemp = _T("test"); 
     char *getTemp=NULL; 

     getTemp = malloc(strTemp.GetLength()+1); 
     strcpy(getTemp, strTemp.GetBuffer(strTemp.GetLength()); 
     printf("결과:%sn", getTemp); 

     free(getTemp); 

2. operator LPCTSTR ()도 마찬가지입니다. 

ex) CString strTemp = _T("test");
     char *getTemp = (LPSTR)(LPCSTR)strData;

CString -> BYTE*
CString str="1234";
BYTE *pbyte;
pbyte = (BYTE(LPSTR)(LPCSTR)str;
 
 
 
CString str = _T("abcd");
BYTE* pbyte = new BYTE[256];
int nSize;
nSize = str.GetLength();
CopyMemory( pbyte, str.GetBuffer(nSize), nSize );
pbyte[nSize] = 0;
 
 
CString  => char* 변환

char * ch;
CString *str;

1) ch = (LPSTR)(LPCSTR)str;
2) ch = str.GetBuffer(str.GetLength());
3) wsprintf( ch, "%s", str);

char*  =>  CString 변환

1) str = (LPCSTR)(LPSTR)ch;
2) str = ch;
 
 
참고)
LPSTR 은 char* 입니다.
LPSTR : char stirng의 32비트 포인터, char* 와 같다.
LPCTSTR : Constant character String의 32비트 포인터
UINT : 32비트 unsigned형 정수
DWORD : unsigned long int형
BYTE : 8비트 unsigned 정수
 
참고 : CString을 const char* 형태로 변경 -> (LPTSTR)(LPCTSTR)CString
 
LPCSTR :  A 32-bit pointer to a constant character string.
LPSTR :  A 32-bit pointer to a character string.
LPCTSTR :  A 32-bit pointer to a constant character string that is portable for Unicode and DBCS.
LPTSTR :  A 32-bit pointer to a character string that is portable for Unicode and DBCS.
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패밀리