﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>学习日记 &#187; 类型转换</title>
	<atom:link href="https://www.softwareace.cn/?cat=92&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>https://www.softwareace.cn</link>
	<description>时刻想着为自己的产品多做一些对他好的事情</description>
	<lastBuildDate>Fri, 20 Mar 2026 06:58:28 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>UnicodeToUTF8</title>
		<link>https://www.softwareace.cn/?p=780</link>
		<comments>https://www.softwareace.cn/?p=780#comments</comments>
		<pubDate>Wed, 23 Apr 2014 02:09:11 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[类型转换]]></category>

		<guid isPermaLink="false">http://www.softwareace.cn/?p=780</guid>
		<description><![CDATA[[crayon-6a2bc668bedd4916104807/] &#160;]]></description>
				<content:encoded><![CDATA[<p></p><pre class="crayon-plain-tag">bool wstring2string(IN const wchar_t* lpwstr, OUT string&amp; str, IN unsigned int uCodePage)
{
	if (NULL == lpwstr)
		return false;

	int nLen = ::WideCharToMultiByte(uCodePage, 0, lpwstr, -1, NULL, 0, NULL, NULL);
	char* pBuf = new char[nLen + 1];
	if (NULL != pBuf)
	{
		nLen = ::WideCharToMultiByte(uCodePage, 0, lpwstr, -1, pBuf, nLen, NULL, NULL);
		pBuf[nLen] = '\0';
		str	= pBuf;
		delete pBuf;		
		return true;
	}

	return false;
}

bool UnicodeToUTF8(IN const wchar_t* lpwstr, OUT string&amp; str)
{
	return wstring2string(lpwstr, str, CP_UTF8);
}</pre><p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.softwareace.cn/?feed=rss2&#038;p=780</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CString2string</title>
		<link>https://www.softwareace.cn/?p=779</link>
		<comments>https://www.softwareace.cn/?p=779#comments</comments>
		<pubDate>Wed, 23 Apr 2014 02:06:59 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[类型转换]]></category>

		<guid isPermaLink="false">http://www.softwareace.cn/?p=779</guid>
		<description><![CDATA[[crayon-6a2bc668bf739691255534/] &#160;]]></description>
				<content:encoded><![CDATA[<p></p><pre class="crayon-plain-tag">void ConvertCString2string(CString&amp; strSrc,std::string&amp; strDes) 
{ 
#ifndef UNICODE 
	strDes = strSrc; 
#else 
	USES_CONVERSION; 
	strDes = W2A(strSrc.LockBuffer()); 
	strSrc.UnlockBuffer(); 
#endif 
}</pre><p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.softwareace.cn/?feed=rss2&#038;p=779</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>wstring2string</title>
		<link>https://www.softwareace.cn/?p=759</link>
		<comments>https://www.softwareace.cn/?p=759#comments</comments>
		<pubDate>Tue, 01 Apr 2014 10:18:01 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[类型转换]]></category>

		<guid isPermaLink="false">http://www.softwareace.cn/?p=759</guid>
		<description><![CDATA[[crayon-6a2bc668bf918057147127/] &#160;]]></description>
				<content:encoded><![CDATA[<p></p><pre class="crayon-plain-tag">std::string ws2s(const std::wstring&amp; ws)
{
    std::string curLocale = setlocale(LC_ALL, NULL);        // curLocale = "C";
    setlocale(LC_ALL, "chs");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = 2 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest,0,_Dsize);
    wcstombs(_Dest,_Source,_Dsize);
    std::string result = _Dest;
    delete []_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}</pre><p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.softwareace.cn/?feed=rss2&#038;p=759</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>string2wstring</title>
		<link>https://www.softwareace.cn/?p=758</link>
		<comments>https://www.softwareace.cn/?p=758#comments</comments>
		<pubDate>Tue, 01 Apr 2014 10:17:27 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[类型转换]]></category>

		<guid isPermaLink="false">http://www.softwareace.cn/?p=758</guid>
		<description><![CDATA[[crayon-6a2bc668bfaec644839572/] &#160;]]></description>
				<content:encoded><![CDATA[<p></p><pre class="crayon-plain-tag">std::wstring s2ws(const std::string&amp; s)
{
    setlocale(LC_ALL, "chs"); 
    const char* _Source = s.c_str();
    size_t _Dsize = s.size() + 1;
    wchar_t *_Dest = new wchar_t[_Dsize];
    wmemset(_Dest, 0, _Dsize);
    mbstowcs(_Dest,_Source,_Dsize);
    std::wstring result = _Dest;
    delete []_Dest;
    setlocale(LC_ALL, "C");
    return result;
}</pre><p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.softwareace.cn/?feed=rss2&#038;p=758</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>string.c_str()</title>
		<link>https://www.softwareace.cn/?p=757</link>
		<comments>https://www.softwareace.cn/?p=757#comments</comments>
		<pubDate>Tue, 01 Apr 2014 10:10:43 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[类型转换]]></category>

		<guid isPermaLink="false">http://www.softwareace.cn/?p=757</guid>
		<description><![CDATA[[crayon-6a2bc668bfcc4391915398/] &#160;]]></description>
				<content:encoded><![CDATA[<p></p><pre class="crayon-plain-tag">Call c_str() to get a const char * (LPCSTR) from a std::string.
It's all in the name:
LPSTR - (long) pointer to string - char *
LPCSTR - (long) pointer to constant string - const char *
LPWSTR - (long) pointer to Unicode (wide) string - wchar_t *
LPCWSTR - (long) pointer to constant Unicode (wide) string - const wchar_t *
LPTSTR - (long) pointer to TCHAR (Unicode if UNICODE is defined, ANSI if not) string - TCHAR *
LPCTSTR - (long) pointer to constant TCHAR string - const TCHAR *
You can ignore the L (long) part of the names -- it's a holdover from 16-bit Windows.</pre><p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.softwareace.cn/?feed=rss2&#038;p=757</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
