데이터 상호 변환(문자열, 색상)
///////////////////////////////////////////////////////////////////
// CString -> int convert
CString strNum = _T("5");
int nNum = _ttoi(strNum);
// int -> CString convert
int nNum = 5;
CString strNum;
strNum.Format(_T("%d"), nNum);
// CString -> double
CString strNum = _T("5.5");
double nNum = _wtof(strNum);
// double -> CString
double nNum = 5.5;
CString strNum;
strNum.Format(_T("%f"), nNum);
// Multibyte 기반일 경우
// _ttoi -> atoi
// _wtof -> atof 로 바꿔주면 된다
///////////////////////////////////////////////////////////////////
CString -> int
CString str(_T("123"));
int num = atoi(LPCTSTR(str));
operator LPCTSTR ( ) const;
CString에 LPCTSTR operator가 정의.
///////////////////////////////////////////////////////////////////
int -> CString
CString str;
int num = 6;
str.Format(_T("%d"),num);
///////////////////////////////////////////////////////////////////
CString -> double
CString str(_T("1.23"));
double num = atof(LPCTSTR(str));
///////////////////////////////////////////////////////
유니코드 <---> 멀티바이트
CString str;
str.Format(_T("유니코드"));
CW2A(str) : 유니코드를 멀티바이트로,
CA2W(str) : 멀티바이트를 유니코드로,
///////////////////////////////////////////////////////
CString <---> Char*형 상호 변환
1) CString --> Char*형
char*형변수=(LPSTR)(LPCSTR)CString형변수;
char*형변수=CString형변수.GetBuffer(CString형변수.Getlength());
2) char*형 --> CString형
CString형변수=(LPCSTR)(LPSTR)char*형변수;
CString형변수.Format("%s",char*형변수);
/////////////////////////////////////////////////////////////////////
CStringW, USES_CONVERSION, A2W, W2A을 활용하여
CHAR 과 WCHAR 자료형의 상호 변환 방법을 설명 드립니다.
/////////////////////////////////////////////////////////////////////
USES_CONVERSION , A2W, W2A 을 이용하는 방법
char sz[10] = "123가나다라마바사"; WCHAR szw[10] = L"123가나다라마바사"; USES_CONVERSION; WCHAR* w = A2W( sz ); // char -> wchar
CHAR* c = W2A( szw ); // wchar -> char |
/////////////////////////////////////////////////////////////////////
CString 과 CStringW 을 이용하는 방법
char sz[10] = "123가나다라마바사"; WCHAR szw[10] = L"123가나다라마바사"; CStringW strw( sz ); // char -> wchar CString str( szw ); // wchar -> char |
/////////////////////////////////////////////////////////////////////
char *, wchar_t *, _bstr_t, CComBSTR, CString, basic_string, System.String 변환
char *
char* orig = "Hello, World!"; size_t origsize = strlen(orig) + 1; // wchar_t * const size_t newsize = 100; size_t convertedChars = 0; wchar_t wcstring[newsize]; mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE); wcscat_s(wcstring, L" (wchar_t *)"); _bstr_t bstrt(orig); // _bstr_t bstr += " (_bstr_t)"; CComBSTR ccombstr(orig); // CComBSTR if(ccombstr.Append(L"(CComBSTR)") == S_OK) CW2A printstr(ccombstr); CString cstring(orig); // CString cstring += " (CString)"; string basicstring(orig); // basic_string basicstring += " (basic_string)"; String* systemstring = gcnew String(orig); // System.String systemstring += " (System::String)"; delete systemstring; |
wchar_t *
wchar_t* orig = L"Hello, World!"; size_t origsize = wcslen(orig) + 1; // char * const size_t newsize = 100; size_t convertedChars = 0; char nstring[newsize]; wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE); strcat_s(nstring, " (char *)"); _bstr_t bstrt(orig); // _bstr_t bstrt += " (_bstr_t)"; CComBSTR ccombstr(orig); // CComBSTR if(ccombstr.Append(L" (CComBSTR)") == S_OK) CW2A printstr(ccombstr); CString cstring(orig); // CString cstring += " (CString)"; wstring basicstring(orig); // basic_string basicstring += L" (basic_string)"; String* systemstring = gcnew String(orig); // System.String systemstring += " (System::String)"; delete systemstring; |
_bstr_t
BSTR을 캡슐화하는 COM 지원 클래스 (comutil.h, comsuppw.lib 또는 comsuppwd.lib)
_bstr_t orig("Hello, World!"); const size_t newsize = 100; // char * char nstring[newsize]; strcpy_s(nstring, (char *)orig); strcat_s(nstring, " (char *)"); wchar_t wcstring[newsize]; // wchar_t * wcscpy_s(wcstring, (wchar_t *)orig); wcscat_s(wcstring, L" (wchar_t *)"); CComBSTR ccombstr((char *)orig); // CComBSTR if(ccombstr.Append(L" (CComBSTR)") == S_OK) CW2A printstr(ccombstr); CString cstring((char *)orig); // CString cstring += " (CString)"; string basicstring((char *)orig); // basic_string basicstring += " (basic_string)"; String* systemstring = gcnew String((char *)orig); // System.String systemstring += " (System::String)"; delete systemstring; |
CComBSTR ATL의 BSTR wrapper 클래스 (atlbase.h)
CComBSTR orig("Hello, World!"); CW2A printstr(orig); const size_t newsize = 100; // char * char nstring[newsize]; CW2A tmpstr1(orig); strcpy_s(nstring, tmpstr1); strcat_s(nstring, " (char *)"); wchar_t wcstring[newsize]; // wchar_t * wcscpy_s(wcstring, orig); wcscat_s(wcstring, L" (wchar_t *)"); _bstr_t bstrt(orig); // _bstr_t bstrt += " (_bstr_t)"; CString cstring(orig); // CString cstring += " (CString)"; wstring basicstring(orig); // basic_string basicstring += L" (basic_string)"; String ^systemstring = gcnew String(orig); // System.String systemstring += " (System::String)"; delete systemstring; |
CString ATL/MFC의 템플릿 클래스인 CStringT의 기본 구현 클래스.
CString orig("Hello, World!"); const size_t newsize = 100; // char * char nstring[newsize]; strcpy_s(nstring, orig); strcat_s(nstring, " (char *)"); // wchar_t * (이 작업을 하려면 먼저 char *로 바꿔야 한다) size_t origsize = strlen(orig) + 1; size_t convertedChars = 0; wchar_t wcstring[newsize]; mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE); wcscat_s(wcstring, L" (wchar_t *)"); _bstr_t bstrt(orig); // _bstr_t bstrt += " (_bstr_t)"; CComBSTR ccombstr(orig); // CComBSTR if(ccombstr.Append(L" (CComBSTR)") == S_OK) CW2A printstr(ccombstr); string basicstring(orig); // basic_string basicstring += " (basic_string)"; String* systemstring = gcnew String(orig); // System.String systemstring += " (System::String)"; delete systemstring; |
basic_string STL 클래스 (<string>)
string orig("Hello, World!"); const size_t newsize = 100; // char * char nstring[newsize]; strcpy_s(nstring, orig.c_str()); strcat_s(nstring, " (char *)"); // wchar_t * (이 작업을 하려면 먼저 char *로 바꿔야 한다) size_t origsize = strlen(orig.c_str()) + 1; size_t convertedChars = 0; wchar_t wcstring[newsize]; mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE); wcscat_s(wcstring, L" (wchar_t *)"); _bstr_t bstrt(orig.c_str()); // _bstr_t bstrt += "(_bstr_t)"; CComBSTR ccombstr(orig.c_str()); // CComBSTR if(ccombstr.Append(L" (CComBSTR)") == S_OK) CW2A printstr(ccombstr); Cstring cstring(orig.c_str()); // CString cstring += " (CString)"; String ^systemstring = gcnew String(orig.c_str()); // System.String systemstring += " (System::String)"; delete systemstring; |
System.String .NET 프레임워크 클래스 (mscorlib)
String ^orig = gcnew String("Hello, World!"); pin_ptr<const wchar_t> wch = PtrToStringChars(orig); size_t origsize = wcslen(wch) + 1; // char * const size_t newsize = 100; size_t convertedChars = 0; char nstring[newsize]; wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE); strcat_s(nstring, " (char *)"); wchar_t wcstring[newsize]; // wchar_t * wcscpy_s(wcstring, wch); wcscat_s(wcstring, L" (wchar_t *)"); _bstr_t bstrt(wch); // _bstr_t bstrt += " (_bstr_t)"; CComBSTR ccombstr(wch); // CComBSTR if(ccombstr.Append(L" (CComBSTR)") == S_OK) CW2A printstr(ccombstr); CString cstring(wch); // CString cstring += " (CString)"; wstring basicstring(wch); // basic_string basicstring += L" (basic_string)"; delete orig; |
/////////////////////////////////////////////////////////////////////
// 색상값 변환
COLORREF <--> CString
char * p;
TCHAR * p;
CString sz=_T("0xFF00FF");
DWORD dw = _tcstol(sz,&p, 16);
COLORREF cr1 =_tcstol(_T("0x1F00FF"),&p, 16); // cr1 = 0x001f00ff
COLORREF cr2 = RGB(0x1F, 0x00, 0xFF); // cr2 = 0x00ff001f
{
long m_nRGBColor = (long)input;
// COLORREF에서 RED, GREEN, BLUE 값을 얻어와서 각각 저장
int m_nRed = (BYTE)m_nRGBColor;
int m_nGreen = (BYTE)(m_nRGBColor >> 8);
int m_nBlue = (BYTE)(m_nRGBColor >> 16);
// String 형식으로 저장
CString m_sRed;
m_sRed.Format(_T("%x"), m_nRed);
CString m_sGreen;
m_sGreen.Format(_T("%x"), m_nGreen);
CString m_sBlue;
// 0 ~ 9값이 나올 경우 두 자리를 유지하기 위해 앞에 0을 붙여줌
if(m_sRed.GetLength() == 1) m_sRed = _T("0") + m_sRed;
if(m_sGreen.GetLength() == 1) m_sGreen = _T("0") + m_sGreen;
if(m_sBlue.GetLength() == 1) m_sBlue = _T("0") + m_sBlue;
return _T("#")+m_sRed+m_sGreen+m_sBlue;
}
2. Convert Hex String value to RGB value
{
CStrng strColor;
int m_nColor[3];
for(int i=0;i<3;i++)
{
iColor[i] = (int)wcstol(strColor, NULL, 16); // 뽑아온 스트링은 16진수 값이므로 이를 10진수로 변경
}
COLORREF output = RGB(iColor[0], iColor[1], iColor[2]);
return output;
}
/* RGB 로 16진수로 변환 */
COLORREF CDlgTabColorRemark_VisibleArea::GetRGB( CString _value )
{
COLORREF aa;
int redValue1 = GetDecimal(colorValue.Mid(2, 1));
int redValue2 = GetDecimal(colorValue.Mid(3, 1));
int greenValue1 = GetDecimal(colorValue.Mid(4, 1));
int greenValue2 = GetDecimal(colorValue.Mid(5, 1));
int blueValue1 = GetDecimal(colorValue.Mid(6, 1));
int blueValue2 = GetDecimal(colorValue.Mid(7, 1));
aa = blueValue1 * pow(16.0, 5.0) + blueValue2 * pow(16.0, 4.0) +
greenValue1 * pow(16.0, 3.0) + greenValue2 * pow(16.0, 2.0) +
redValue1 * 16.0 + redValue2 ;
return aa ;
}
/* 16진수 스트링 값을 RGB로 변환 */
DWORD dw = _tcstol(ResultQry[c].GetAt(0),&p, 16);
m_refUseColor[c] =_tcstol(ResultQry[c].GetAt(0),&p, 16);
/* String to RGB */
COLORREF StringToRGB(const TCHAR* pColorRef)
{
int i=0;
int ar[3];
int nLen = lstrlen(pColorRef)+1;
TCHAR* pBuf = (TCHAR*)malloc(nLen*sizeof(TCHAR));
lstrcpy(pBuf, pColorRef);
TCHAR* p = _tcstok(pBuf, _T(","));
while(p)
{
ar[i++] = _tstoi(p);
p = _tcstok(NULL, _T(","));
}
free(pBuf);
return RGB(ar[0], ar[1], ar[2]);
}
/////////////////////////////////////////////////////////////////////
int <--> BYTE
int -> BYTE(unsigned char)
numberVal[0] = (BYTE)((nVal)&0xff);
numberVal[1] = (BYTE)((nVal>>8)&0xff);
numberVal[2] = (BYTE)((nVal>>16)&0xff);
numberVal[3] = (BYTE)((nVal>>24)&0xff);
BYTE -> int
nVal |= (int)((decryptedData[0])&0xff);
nVal |= (int)((decryptedData[1]<<8)&0xff00);
nVal |= (int)((decryptedData[2]<<16)&0xff0000);
nVal |= (int)((decryptedData[3]<<24)&0xff000000);
/////////////////////////////////////////////////////////////////////
'C & C++ > C & C++' 카테고리의 다른 글
CArray를 이용한 배열 선언 이해 (0) | 2011.02.11 |
---|---|
[Etc] 특정 프로세스 cpu 점유율 체크 (0) | 2011.01.26 |
[멤버함수] MFC 주요 클래스 멤버함수, 변수 (0) | 2011.01.26 |
[Etc] HDD 용량출력 (0) | 2011.01.12 |
rand() 함수 (0) | 2010.04.26 |
댓글