'C/C++'에 해당되는 글 2건

  1. 2007년 04월 04일 String Conversions - 소류
  2. 2007년 04월 02일 Const - 소류

String Conversions

String to Hex

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

CString MyString = "ABCDEF"; char * szMyString = (char *) (LPCTSTR) MyString;
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);

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 


LPSTR ->BSTR

BSTR bstr;
CString temp;
LPSTR lpstr;
temp = lpstr;
bstr = temp.AllocSysString();


BSTR -> int

CString str = CString(width);
this->m_width = atoi(str);


CString -> BSTR

BSTR bsFile = CString( pszFile ).AllocSysString();


CString -> char*

char * pszValue = NULL;

CString strValue = "AAA";

pszValue = (LPSTR)(LPCTSTR)strValue;


BSTR -> char*

char* BSTRtoChar(BSTR in)
{
 char* pChar = (char*)malloc(_bstr_t(in).length());
 strcpy(pChar,(char*)_bstr_t(in));
 SysFreeString(in);
 return pChar;
}


LPCTSTR -> int

int op1 = atoi((LPCTSTR) m_op1);
int op2 = atoi((LPCTSTR) m_op2);


int ->LPCTSTR

CString s;
s.Format(_T("%0.*f"),(int)nPos);


CString -> WCHAR

WCHAR       wstring[1024];
ZeroMemory(wstring, sizeof(wstring));
mbstowcs(wstring, (LPCTSTR)fullPath, fullPath.GetLength());

크리에이티브 커먼즈 라이센스
Creative Commons License
2007년 04월 04일 10시 21분 2007년 04월 04일 10시 21분

Const

1. 변수
예)
    const i = 100;
i 값 변경불가

2. 포인터형 : 기본적으로 2가지 형태가 있을 수 있음. 그외 여려형태가 가능
예1) 값은 변경 불가능하지만 주소는 변경가능한 형태
    int temp = 100, temp2 = 200;
    const int *ipConst = &temp;  // *ipConst 값 변경 불가, ipConst(주소)값은 변경가능
    // int const *ipConst = &temp;  // 이런형태로 써도 위와 같은 의미

    // *ipConst = 300;   // 불가능한 형태
    ipConst = &temp2;  // 가능한 형태

예2) 주소는 변경 불가능하지만 값은 변경가능한 형태
    int temp = 100, temp2 = 200;
    int * const iConstp = &temp;  // *iConstp 값 변경 가능, iConstp(주소)값은 변경불가

    *iConstp = 300;   // 가능한 형태
    //iConstp = &temp2;  // 불가능한 형태

주의 : const가 결합되는 위치가 값인지 주소인지에 유의


3. 참조형
예1) 직접적으로 값과 주소 모두 변경 불가능하지만 참조 원본을 통한 값변경은 가능한 경우
   int temp3 = 100, temp5 = 200;
   int const &ircVal = temp3;  
  
   //ircVal = 2000; // 컴파일 에러 발생 (const 참조는 값 변경불가)
   //ircVal = temp5;  // 주소도  변경불가
   temp3 = 9000;  // 참조 원본은 변경가능, 결과적으로 ircVal의 값도 변하게됨
   

예2) 직접적으로 값과 주소 모두 변경 가능하지만 참조가 가르키는 값은 변화가 없는경우
    int temp4 = 300, temp5 = 500;
    int & const icrVal = temp4;

    icrVal = 6000;  // 값변경 가능, 하지만 값에 변경이 안됨  
    cout << " icrVal " << icrVal << endl;  // 여전히 300이 찍힘
    icrVal = temp5; // 주소도 변경가능 역시 값에 변경이 안됨
    cout << " icrVal " << icrVal << endl;  // 여전히 300이 찍힘

4. 함수 : class의 멤버함수인 경우만 const 함수 사용가능. 해당 class의 멤버변수를 변경할수 없음.
예)
class ConstTest
{
public:
        int m_iA;

        ConstTest()
        {        m_iA = 1;           }
        int const_func1( int &a_iA,  int &a_iB) const
        {
                int a = 1;
                int b = 2;
                int c = 0;

                c = a + b;
                a_iA += 100;  
                // m_iA += 100; // 에러발생. 멤버변수는 변경 불가  
                return m_iA;
        }
};
               

5. 클레스
예)
  const CMyConstClass CC;  
 
  // 내부 멤버변수 전체를 변경불가능한 클레스,(생성자 함수만은 예외)
  // 모든 내부 멤버 함수는 기본적으로 const 함수가 되야만함.
  // 내부 함수의 지역 변수및 인자로 받은 변수는 변경가능.
크리에이티브 커먼즈 라이센스
Creative Commons License
2007년 04월 02일 01시 19분 2007년 04월 02일 01시 19분