1. 程式人生 > >C++ Builder : string->char* wstring->char*

C++ Builder : string->char* wstring->char*

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);