소스 코드를 GitHub로 옮김
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | #pragma once #ifndef _TYPE_CASTER_H_ #define _TYPE_CASTER_H_ #include <Windows.h> #include <iostream> #include <string> #include <vector> #include <atlstr.h> #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS class KTypeCaster { public: /* CString formating This code block {@code int num = 32 CString strTemp; strTemp.Format(_T("I am the %d years old"), nYear); AfxMessageBox(strTemp); } can change to {@code int nYear = 32 AfxMessageBox(CstFormat(_T("I am the %d years old"), nYear)); } @see http://blog.daum.net/kwonjunsung/34 */ static CString CstFormat(const CString& strMsg, ...) { CString strRet; va_list valist; va_start(valist, strMsg); strRet.FormatV(strMsg, valist); va_end(valist); return strRet; } // CString <- std::string static CString ConvertString_StdStr2Cstr(const std::string src) { CString result = CA2CT(src.c_str()); return result; } // CString <- std::string static CString ConvertString_StdStr2Cstr_2(const std::string src) { return CString(src.c_str()); } /* // LPTSTR <- CString @see https://docs.microsoft.com/ko-kr/cpp/atl-mfc-shared/cstring-operations-relating-to-c-style-strings !!Need to delete when program exit!! */ static void ConverString_Cstr2Lptstr(const CString src, LPTSTR *result) { int sizeOfString = src.GetLength() + 1; *result = new TCHAR[sizeOfString]; _tcscpy_s(*result, sizeOfString, src); } /* std:wstring <- std::string */ static std::wstring ConvertString_StdStr2StdWstr(const std::string src) { std::wstring result = CA2CT(src.c_str()); return result; } /** std:wstring <- std::string {@code string = "COM3"; wstring stemp; LPCWSTR result_port; stemp = s2ws(port_nr); result_port = stemp.c_str(); // now passing result_port to my function i am getting success } @see https://stackoverflow.com/a/22713047/7017299 (std::string -> LPCTSTR) */ static std::wstring ConvertString_StdStr2StdWstr_2(const std::string &source) { int soureLength = (int)source.length() + 1; int len = MultiByteToWideChar(CP_ACP, 0, source.c_str(), soureLength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, source.c_str(), soureLength, buf, len); std::wstring r(buf); delete[] buf; return r; } /* std:wstring <- CString */ static std::wstring ConvertString_Cstr2StdWstr(const CString src) { std::wstring result = src.operator LPCWSTR(); return result; } //std::string <- std::wstring static std::string ConvertString_StdWstr2StdStr(const std::wstring src) { std::string result = CT2CA(src.c_str()); return result; } //std::string <- CString static std::string ConvertString_Cstr2StdStr(const CString src) { std::string result = CT2CA(src.operator LPCWSTR()); return result; } //std::string <- TCHAR * static std::string ConvertString_Tchar2StdStr(const TCHAR* ptsz) { CString result_cst(ptsz); return ConvertString_Cstr2StdStr(result_cst); } #ifdef UNICODE static DWORD Convert_Cstr2Dword(CString src) { DWORD result = _ttol(src); return result; } static int Convert_Cstr2Int(CString src) { int result = _ttoi(src); return result; } static double Convert_Cstr2Double(CString src) { double result = _wtof(src); return result; } #else static DWORD Convert_Cstr2Dword(CString src) { DWORD result = atol(src); return result; } static int Convert_Cstr2Int(CString src) { int result = atoi(src); return result; } static double Convert_Cstr2Double(CString src) { double result = atof(src); return result; } #endif // !UNICODE } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | void CMyCustomDialogBaseDlg::OnBnClickedBtnTestConvert() { //문자 집합(유니코드, 멀티바이트)이 다를 경우 //사용해야 하는 함수가 다를 수 있음 //For test in Visual studio 2017 (Unicode) CString cstr(_T("나는 CString")); std::string str("나는 string"); std::wstring wstr(_T("나는 wstring")); CString cst_int = _T("141432"); CString cst_double = _T("141432.55"); DWORD dword = 314159; //DWORD 사이즈는 4byte (LONG으로 변환) int number = 119; double double_num = 3.14159265358979; /////////////////////////////////////////////////// //To Cstring/////////////////////////////////////// // CString <- std::string CString cstr2 = KTypeCaster::ConvertString_StdStr2Cstr(str); CString cstr3 = KTypeCaster::ConvertString_StdStr2Cstr_2(str); // CString <- std::wstring CString cstr4 = wstr.c_str(); // CString <- int CString cstr5 = KTypeCaster::CstFormat(_T("Integer : %d"),number); // CString <- double CString cstr6 = KTypeCaster::CstFormat(_T("Double : %f"), double_num); ///////////////////////////////////////////////// /////////////////////////////////////////////////// //LPTSTR <- CString//////////////////////////////// CString cstr7 = _T("This is a test"); LPTSTR lpTstr; KTypeCaster::ConverString_Cstr2Lptstr(cstr7, &lpTstr); delete lpTstr; /////////////////////////////////////////////////// ////////////////////////////////////////////////// //To std::wstring///////////////////////////////// // std::wstring <- CString std::wstring wstr2 = KTypeCaster::ConvertString_Cstr2StdWstr(cstr); std::wstring wstr3 = cstr.GetBuffer(0); // std::wstring <- std::string std::wstring wstr4 = KTypeCaster::ConvertString_StdStr2StdWstr(str); std::wstring wstr5 = KTypeCaster::ConvertString_StdStr2StdWstr_2(str); //std::string -> LPCTSTR ///////////////////////////////////////////////// ////////////////////////////////////////////////// //To std::string///////////////////////////////// // std::string <- CString std::string str2 = KTypeCaster::ConvertString_Cstr2StdStr(cstr); // std::string <- std::wstring std::string str3 = KTypeCaster::ConvertString_StdWstr2StdStr(wstr); ///////////////////////////////////////////////// ////////////////////////////////////////////////// //To LPWSTR///////////////////////////////// // LPWSTR <- CString LPWSTR lpwstr = (LPWSTR)(LPCTSTR)cstr; ///////////////////////////////////////////////// ////////////////////////////////////////////////// //To const char* ///////////////////////////////// /* const char* <- std::string @see http://www.techiedelight.com/convert-std-string-const-char-cpp/ */ // const char* <- std::string const char *c_char_1 = str.c_str(); const char *c_char_2 = str.data(); // const char* <- std::string (over C++11) const char *c_char_3 = &str[0]; const char *c_char_4 = &*str.begin(); // const char* <- CString const char *c_char_5 = (CStringA)cstr; ///////////////////////////////////////////////// ////////////////////////////////////////////////// //To DWORD ////////////////////////////////////// // DWORD <- CString DWORD dword2 = KTypeCaster::Convert_Cstr2Dword(cst_int); // CString <- DWORD CString cst_number2 = KTypeCaster::CstFormat(_T("%lu"), (ULONG)dword); ///////////////////////////////////////////////// ////////////////////////////////////////////////// //To integer///////////////////////////////////// // int <- CString int number_2 = KTypeCaster::Convert_Cstr2Int(cst_int); //////////////////////////////////////////////// ////////////////////////////////////////////////// //To double///////////////////////////////////// // double <- CString double double_num_2 = KTypeCaster::Convert_Cstr2Double(cst_double); //////////////////////////////////////////////// } | cs |
TCHAR는 가능하면 쓰지 말자...
참고사이트 :
http://adnoctum.tistory.com/749
http://easyprogramming.tistory.com/entry/이-한장으로-문자열-마스터
http://blog.naver.com/PostView.nhn?blogId=jtyworld&logNo=90043807453
'Study > C, C++ (MFC, UE)' 카테고리의 다른 글
[MFC] 도구상자 목록 (vs2017 기준) (0) | 2018.07.03 |
---|---|
GDI+을 사용하려면 반드시 초기화 구문을 쓰자 (0) | 2018.06.25 |
[C++] Vector<String> 문자열 소트 (0) | 2018.05.17 |
[MFC] Dialog 기반 프로그램 enter,esc 키 처리에 관해 (0) | 2018.05.15 |
OpenGL 함수 (진행 중.....) (0) | 2018.05.10 |