C++ String: How to convert between 'CString' and 'std::string'?
2009-07-08 14:38
513 查看
'CString' to 'std::string':
std::string cannot always construct from a LPCTSTR i.e. the code will fail for UNICODE builds.
So the following conversion will lead to error:
CString cs("Hello");
std::string s((LPCTSTR)cs);
As std::string can construct only from LPSTR / LPCSTR, a programmer who uses VC++ 7.x or better can utilize conversion classes such as CT2CA as an intermediary.
CString cs ("Hello");
// Convert a TCHAR string to a LPCSTR
CT2CA pszConvertedAnsiString (cs);
// construct a std::string using the LPCSTR input
std::string strStd (pszConvertedAnsiString);
'std::string' to 'CString':
std::string s("Hello");
CString cs(s.c_str());
std::string cannot always construct from a LPCTSTR i.e. the code will fail for UNICODE builds.
So the following conversion will lead to error:
CString cs("Hello");
std::string s((LPCTSTR)cs);
As std::string can construct only from LPSTR / LPCSTR, a programmer who uses VC++ 7.x or better can utilize conversion classes such as CT2CA as an intermediary.
CString cs ("Hello");
// Convert a TCHAR string to a LPCSTR
CT2CA pszConvertedAnsiString (cs);
// construct a std::string using the LPCSTR input
std::string strStd (pszConvertedAnsiString);
'std::string' to 'CString':
std::string s("Hello");
CString cs(s.c_str());
相关文章推荐
- Part 59 to 60 Difference between Convert ToString and ToString,String and StringBuilder
- How to convert QString to std::string?
- 【Z一贴】C++ String: How to convert a string into a numeric type?
- C++ convert between string and CString (LPWSTR)
- How to convert std::string to LPCWSTR in C++ (Unicode)
- UnicodeToMultiByte,ConvertBSTRToString,std::string,CString的区别
- convert between char* and std::string
- How To Convert Virtual Machines Between VirtualBox and VMware[测试可行]
- How can I convert String to Int in C#?
- how convert large HEX string to binary array ?
- How to get URL and QueryString value in an ASP.NET page
- Convert IP addresses from a dots-and-number string to a struct in_addr and back
- java bean convert to json string and json string convert to java bean
- how to convert wstring to string
- error C2440: 'initializing' : cannot convert from 'boost::filesystem3::path' to 'std::basic_string<_
- How can I convert a QString to char* and vice versa?
- How to Switch Between GDM and KDM on Ubuntu
- DER vs. CRT vs. CER vs. PEM Certificates and How To Convert Them
- C# How to Convert String to ASCII OR how to convert ASCII To String
- Convert Between char* string to wchar_t* string