Showing posts with label CString. Show all posts
Showing posts with label CString. Show all posts

2009/03/05

UTF8 문자열을 std::string 이나 CString 으로 변환하기

다국어 프로그래밍에서 RapidXML 을 이용하다가 필요하여 만들어 본 문자열 변환 함수 이다.



////////////////////////////////////////////////////////////////
// UTF8 문자열은 std::string 으로 변환하기
void CMultiLang::UTF82A( IN const char* utf8, OUT string& ansi )
{
int length = MultiByteToWideChar( CP_UTF8, 0, utf8, (int)strlen(utf8) + 1, NULL, NULL );
wchar_t* pBuf = new wchar_t[ length + 1 ];

MultiByteToWideChar( CP_UTF8, 0, utf8, (int)strlen(utf8) + 1, pBuf, length );

pBuf[length] = 0;
ansi = CStringA( pBuf );

delete [] pBuf;
}

////////////////////////////////////////////////////////////////
// UTF8 문자열은 CString 으로 변환하기
void CMultiLang::UTF82T( IN const char* utf8, OUT CString& out )
{
int length = MultiByteToWideChar( CP_UTF8, 0, utf8, (int)strlen(utf8) + 1, NULL, NULL );
wchar_t* pBuf = new wchar_t[ length + 1 ];

MultiByteToWideChar( CP_UTF8, 0, utf8, (int)strlen(utf8) + 1, pBuf, length );

pBuf[length] = 0;
out = CString( pBuf );

delete [] pBuf;
}

Original Post : http://neodreamer-dev.tistory.com/264

2009/02/10

[MFC] CString 의 문자/문자열 삭제 및 치환하기

CString 의 멤버 함수인 Remove 와  Replace 의 활용 예이다.

Remove 함수는 문자열에서 지정된 문자를 삭제하고 Replace 는 문자열에서 지정된 문자 또는 문자열을 다를 문자나 문자열로 치환한다.

    strValue = _T("Hello World!!");

int nCh = strValue.Replace( _T('l'), _T('w') );
TRACE2( "Replaced %d : %s
\n", nCh, strValue );

nCh = strValue.Replace( _T("ww"), _T("ll") );
TRACE2( "Replaced %d : %s
\n", nCh, strValue );

nCh = strValue.Remove( _T('!') );
TRACE2( "Removed %d : %s
\n", nCh, strValue );

#결과
Replaced 3 : Hewwo Worwd!!
Replaced 1 : Hello Worwd!!
Removed 2 : Hello Worwd



MSDN 사이트에서 발췌한 Replace 사용 예.

http://msdn.microsoft.com/en-us/library/aa300582(VS.60).aspx

//First example, with old and new equal in length.

CString strZap("C--");
int n = strZap.Replace('-', '+');
ASSERT(n == 2);
ASSERT(strZap == "C++");

//Second example, old and new are of different lengths.

CString strBang("Everybody likes ice hockey");
n = strBang.Replace("hockey", "golf");
ASSERT(n == 1);
n = strBang.Replace("likes", "plays");
ASSERT(n == 1);
n = strBang.Replace("ice", NULL);
ASSERT(n == 1);
ASSERT(strBang == "Everybody plays golf");

// note that you now have an extra space in your
// sentence. To remove the extra space, include it
// in the string to be replaced, i.e.,"ice ".



MSDN 사이트에서 발췌한 Remove 사용 예.

http://msdn.microsoft.com/en-us/library/aa300579(VS.60).aspx

//remove the lower-case letter 't' from a sentence:

CString str("This is a test.");
int n = str.Remove('t');
ASSERT(n == 2);
ASSERT(str == "This is a es.");

Original Post : http://neodreamer-dev.tistory.com/250

2008/12/11

CString::Format 으로 삽질하다.

CString 의 Format 문 때문에 종일 삽질을 했다.

    const CString INSERT_MODEL =
_T("INSERT INTO MODEL ")
_T(" (")
_T(" `1`,")
_T(" `2`,")
_T(" `3`,")
_T(" `4`,")
_T(" `5`,")
_T(" `6`,")
_T(" `7`,")
_T(" `8`,")
_T(" `9`,")
_T(" `0`")
_T(" ) VALUES (")
_T(" '%s',") // M
_T(" %f, ") // P
_T(" %f, ") // P
_T(" %f, ") // S
_T(" '%s',") // M
_T(" '%s',") // M
_T(" %d, ") // I
_T(" %f, ") // D
_T(" '%s',") // L
_T(" '%s' ") // M
_T(" );");

CString strSQL;
strSQL.Format( INSERT_MODEL
, CString( "g800" ) // '%s',
, 190.999f // %f,
, 89.999f // %f,
, 0.000f // %f,
, _T("") // '%s',
, CString( "" ) // '%s',
, 0 // %d,
, 0 // %f,
, _T("") // '%s',
, CString( "" ) // '%s'
);

AfxMessageBox( strSQL );




정말 문제가 없어보이는 코드인데... (기본이 부족한 것일까?)


문제가 되었던 부분은 36번째 줄의 실수형을 넣어주어야 하는곳에 정수형 0을 넣어준게 문제였다.

정수형 0을 넣고 실행을 하면 아래와 같은 에러 메시지가 출력된다.



실수형으로 (0.0f) 넣고 실행을 하면 아무런 문제가 없이 정상 동작을 하였다

Original Post : http://neodreamer-dev.tistory.com/216