string類型轉換成LPCTSTR

來源:互聯網
上載者:User

參見以下連結:

http://blog.sina.com.cn/s/blog_5d51f4e90100z86n.html

http://bbs.csdn.net/topics/220074393

LPCTSTR不是一個類型,而是兩種類型:LPCSTR和LPCWSTR其中之一。會根據你當前程式是否使用UNICODE字元集來變成那二者之一。如果使用UNICODE字元集,則LPCTSTR = LPCWSTR,否則LPCTSTR = LPCSTR。

 

標準庫的std::string轉換成LPCSTR很簡單:直接調用c_str()即可。例:

std::string a="abc";

LPCSTR str = a.c_str();

 

標準庫還有一個wstring,代表寬字元的string,std::wstring轉換成LPCWSTR也一樣很簡單:

std::wstring a = L"abc";

LPCWSTR str = a.c_str();

 

如果要是std::string轉換成LPCWSTR或者std::wstring轉換成LPCSTR那就比較麻煩了,需要調用MultiByteToWideChar或WideCharToMultiByte進行字元集之間的轉換。不過大多數時候不需要這種交叉轉換,一個程式一般只會使用一種字元集。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.