1. char*->string
(1)直接转换
const char* nodename; string temp = nodename;
string temp2(nodename);
2. wchar_t*->wstring
(1)直接转换
const wchar_t* nodename; wstring temp = nodename;
wstring temp2(nodename);
3. string->char*
wstring->char*
方法一:
(1)将wstring.c_str()转为wchar_t*
方法二:
(1)将string.c_str(), wstring.c_str()转为AnsiString(http://blog.csdn.net/bannico/article/details/7577728),
(2)将AnsiString.c_str()转为char*
方法一:
string temp;
const char* nodename = temp.c_str();
方法二:
struct IPAddress{ std::wstring hostname; std::vector<std::wstring> ips; }; scripting::IPAddress dns = (*pPreloadDns)[i]; AnsiString strA = dns.hostname.c_str(); char * hn = strA.c_str();
但据说这样很不可靠(http://bbs.csdn.net/topics/30479944),安全的做法是:
方法三:
String str="123456"; int len=str.Length(); char *cp=new char[len+1]; StrPCopy(cp, str);
4. wstring->wchar_t*
string->w_char*(参见5)
方法一:
(1)将wstring.c_str()转为wchar_t*
方法二.一:
(1)将wstring.c_str()转为UnicodeString
(2)将UnicodeString.c_str()转为wchar_t*
方法二.二:
(1)将wstring.c_str()转为AnsiString
(2)使用ANSIToUnicode方法将AnsiString.c_str()转为wchar_t*
方法一:
wstring temp;
wchar_t* nodename = temp.c_str();
方法二.一:
struct IPAddress{ std::wstring hostname; std::vector<std::wstring> ips; }; scripting::IPAddress dns = (*pPreloadDns)[i]; UnicodeString strA = dns.hostname.c_str(); //char * hn = strA.c_str(); wchar_t * hn = strA.c_str();
方法二.二:
struct IPAddress{ std::wstring hostname; std::vector<std::wstring> ips; }; scripting::IPAddress dns = (*pPreloadDns)[i]; AnsiString strA = dns.hostname.c_str(); //char * hn = strA.c_str(); wchar_t * hn = ANSIToUnicode(strA.c_str());
5. char*->UnicodeString->wstring, wchar_t*
(1)将char*转为UnicodeString
(2)将UnicodeString.c_str()转为wstring, wchar_t*
UnicodeString temp(inet_ntoa(pAddr->sin_addr)) ; //inet_ntoa returns char*
std::wstring addr(temp.c_str());
wchar_t* addr2 = temp.c_str();
log_debug(_T("set connection ip:") << addr); con->setIP(addr);
6. wchar_t*->AnsiString->string, char*
(1)将wchar_t*转为AnsiString
(2)将AnsiString.c_str()转为string, char*
wchar_t* str = wstring.c_str();
AnsiString temp(str ) ; //inet_ntoa returns char*
std::string addr(temp.c_str());
char* addr2 = temp.c_str();
log_debug(_T("set connection ip:") << addr); con->setIP(addr);
下面关于string,wstring互转的方法是错误的。对ansi字符可能能得到正确结果,但如果传入的参数是“中文”字符串,将得不到正确的结果。
7. string->wstring
static wstring Str2Wstr (string str ) { if (str.length() == 0) return L""; std::wstring wstr; wstr.assign (str.begin(), str.end()); return wstr; }
8. wstring->string
static string Wstr2Str(wstring wstr ) { if (wstr.length() == 0) return ""; std::string str; str.assign (wstr.begin(), wstr.end()); return str; }
正确的做法是使用MultiByteToWideChar和WideCharToMultiByte:
9. string->wstring
std::wstring stringToWstring(const std::string& str) { LPCSTR pszSrc = str.c_str(); int nLen = MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, NULL, 0); if (nLen == 0) return std::wstring(L""); wchar_t* pwszDst = new wchar_t[nLen]; if (!pwszDst) return std::wstring(L""); MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, pwszDst, nLen); std::wstring wstr(pwszDst); delete[] pwszDst; pwszDst = NULL; return wstr; }
10. wstring->string
std::string wstringToString(const std::wstring& wstr) { LPCWSTR pwszSrc = wstr.c_str(); int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL); if (nLen == 0) return std::string(""); char* pszDst = new char[nLen]; if (!pszDst) return std::string(""); WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL); std::string str(pszDst); delete[] pszDst; pszDst = NULL; return str; }
测试代码:
int _tmain(int argc, _TCHAR* argv[]) { std::string str = "中文"; std::wstring wstr = L"中文"; std::string str1 = ""; std::wstring wstr1 = L""; LPCSTR pSrc = "中文"; LPCWSTR pwzSrc = L"中文"; stringToWstring(str); wstringToString(wstr); stringToWstring(str1); wstringToString(wstr1); stringToWstring(pSrc); wstringToString(pwzSrc); return 0; }