1. 程式人生 > >C++ wstring string char* wchar_t相互轉換

C++ wstring string char* wchar_t相互轉換

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

Original URL: http://www.cnblogs.com/zhcncn/archive/2013/05/20/3089084.html