[MFC] CString 과 숫자간 변환
본문 바로가기

응용 프로그램 개발/C++, MFC, Windows

[MFC] CString 과 숫자간 변환

728x90
반응형

1. 문자열에 변수(숫자) 포함하기

다음과 같이 문자열을 숫자로 변환하거나 문자열에 숫자를 포함시킬 수 있다.

//숫자를 문자로 변환
int n;
CString strText;
strText.Format(_T("%d"), n);


//문자열에 변수(숫자) 포함하기
int n;
CString strText;
strText.Format(_T("text%d"), n);

 

2. 문자열을 숫자(정수)로 변환

CString strText = "1234";
int n = atoi(CStringA(strText));

 

CString strText = "1234";
int n = _ttoi(strText);

 

 

+)

확장자명 제거

다음과 같이 확장자명을 제거하여 파일명을 출력하거나 파일명을 바꿀 수 있다.

// 파일명 출력
int nPos = m_strInputPath.ReverseFind(L'.xlsx');
CString strInputPath = m_strInputPath.Left(nPos);

// 파일명 바꾸기
int nPos = m_strInputPath.ReverseFind(L'.xlsx');
CString strInputPath = m_strInputPath.Left(nPos) + L".xlsx";

 

 

728x90
반응형