<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672588</guid>
        <pubDate>Thu, 02 Jun 2016 07:05:03 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672588</link>
        <description><![CDATA[progman: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=291288&view=findpost&p=3672216'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Ataru &#064; <time class="tag-quote__quoted-time" datetime="2016-05-29T20:00:03+03:00">29.05.16, 17:00</time></span><div class='quote '>И почему названия функций и переменных такие странные? utf8 это и есть юникод. Корректнее было бы назвать Utf8ToASCII(), мне во всяком случае нужно именно такое преобразование </div></div><br>
конвертация зависит от флагов у APIшной функции MultiByteToWideChar,  в моем случае это CP_UTF8 а вам надо CP_ACP<br>
<br>
<div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=291288&view=findpost&p=3672216'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Ataru &#064; <time class="tag-quote__quoted-time" datetime="2016-05-29T17:00:03+00:00">29.05.16, 17:00</time></span><div class='quote '>Корректнее было бы назвать Utf8ToASCII()</div></div><br>
С флагом CP_ACP, соглашусь, было бы корректнее. но у меня именно UTf8 &lt;-&gt; UTF16]]></description>
        <author>progman</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672222</guid>
        <pubDate>Sun, 29 May 2016 20:09:10 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672222</link>
        <description><![CDATA[amk: Добавляемые символы - это BOM (Byte Order Mark). Показывают порядок байт в широких символах. Добавляются скорее всего текстовым редактором (виндовый Notepad так делает), хотя в файле с кодировкой UTF-8 их ставить не обязательно. По стандарту их наоборот не должно быть.<br>WideChar и MultyByte это исторические названия, пришли из времён, когда названий UNICODE и UTF-8 ещё не придумали.]]></description>
        <author>amk</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672216</guid>
        <pubDate>Sun, 29 May 2016 17:00:03 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672216</link>
        <description><![CDATA[Ataru: Этот способ тоже добавляет в начало символы и тоже только при первом преобразовании. Из &quot;text&quot; делает &quot;ï»¿text&quot;<br>
<br>
И почему названия функций и переменных такие странные? utf8 это и есть юникод. Корректнее было бы назвать Utf8ToASCII(), мне во всяком случае нужно именно такое преобразование <br>
<br>
<span class="tag-color tag-color-named" data-value="mergepost" style="color: mergepost"><span class='tag-size' data-value='7' style='font-size:7pt;'>Добавлено <time class="tag-mergetime" datetime="2016-05-29T17:46:57+00:00">29.05.16, 17:46</time></span></span><br>
Все, сорри, вопрос снимается. Этот первый символ оказывается раньше появляется, т.е. проблема в другом месте. Просто в отладчике его не видно, если смотреть значение wstring, но он есть в буфере, из которого эта wstring создается.]]></description>
        <author>Ataru</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672187</guid>
        <pubDate>Sun, 29 May 2016 03:42:05 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672187</link>
        <description><![CDATA[progman: <strong class='tag-b'>Ataru</strong>, вот еще вариант на винапи<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">void utf16to8( const std::wstring&amp; utf16, std::string&amp; utf8 )</div><div class="code_line">{</div><div class="code_line">&nbsp;&nbsp; &nbsp;char &nbsp; &nbsp;mUtf8[ MAX_PATH ] = { 0 };</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;int len = WideCharToMultiByte(CP_UTF8, 0, utf16.c_str(), -1, NULL, 0, 0, 0);</div><div class="code_line">&nbsp;&nbsp; &nbsp;if (len &#62;= 1 &amp;&amp; len &#60; MAX_PATH )</div><div class="code_line">&nbsp;&nbsp; &nbsp;{ </div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;WideCharToMultiByte(CP_UTF8, 0, utf16.c_str(), -1, mUtf8, len, 0, 0);</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;utf8 = mUtf8;</div><div class="code_line">}</div><div class="code_line">&nbsp;</div><div class="code_line">void utf8to16(const std::string&amp; utf8, std::wstring&amp; utf16)</div><div class="code_line">{</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;wchar_t mUtf16[ MAX_PATH ] = { 0 };</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;int len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);</div><div class="code_line">&nbsp;&nbsp; &nbsp;if ( len &#62;= 1 &amp;&amp; len &#60; MAX_PATH )</div><div class="code_line">&nbsp;&nbsp; &nbsp;{ </div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, mUtf16, len );</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;utf16 = mUtf16;</div><div class="code_line">}</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script>]]></description>
        <author>progman</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672104</guid>
        <pubDate>Fri, 27 May 2016 19:38:17 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=3672104</link>
        <description><![CDATA[Ataru: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=291288&view=findpost&p=2463388'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Masterkent &#064; <time class="tag-quote__quoted-time" datetime="2009-12-30T14:08:08+00:00">30.12.09, 14:08</time></span><div class='quote '><div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=291288&view=findpost&p=2463185'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Alca &#064; <time class="tag-quote__quoted-time" datetime="2009-12-30T10:29:00+00:00">30.12.09, 10:29</time></span><div class='quote '>Преобразование std::string в std::wstring туда и обратно, как делается? </div></div><br>
Стандартными средствами C++ примерно так можно:<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">#include &#60;iostream&#62;</div><div class="code_line">#include &#60;locale&#62;</div><div class="code_line">#include &#60;string&#62;</div><div class="code_line">#include &#60;vector&#62;</div><div class="code_line">&nbsp;</div><div class="code_line">std::wstring wide_string(std::string const &amp;s, std::locale const &amp;loc)</div><div class="code_line">{</div><div class="code_line">&nbsp;&nbsp; &nbsp;if (s.empty())</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return std::wstring();</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::ctype&#60;wchar_t&#62; const &amp;facet = std::use_facet&#60;std::ctype&#60;wchar_t&#62; &#62;(loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp;char const *first = s.c_str();</div><div class="code_line">&nbsp;&nbsp; &nbsp;char const *last = first + s.size();</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::vector&#60;wchar_t&#62; result(s.size());</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;facet.widen(first, last, &amp;result[0]);</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;return std::wstring(result.begin(), result.end());</div><div class="code_line">}</div><div class="code_line">&nbsp;</div><div class="code_line">std::string narrow_string(std::wstring const &amp;s, std::locale const &amp;loc, char default_char = &#39;?&#39;)</div><div class="code_line">{</div><div class="code_line">&nbsp;&nbsp; &nbsp;if (s.empty())</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return std::string();</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::ctype&#60;wchar_t&#62; const &amp;facet = std::use_facet&#60;std::ctype&#60;wchar_t&#62; &#62;(loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp;wchar_t const *first = s.c_str();</div><div class="code_line">&nbsp;&nbsp; &nbsp;wchar_t const *last = first + s.size();</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::vector&#60;char&#62; result(s.size());</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;facet.narrow(first, last, default_char, &amp;result[0]);</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;return std::string(result.begin(), result.end());</div><div class="code_line">}</div><div class="code_line">&nbsp;</div><div class="code_line">char const *russian_locale_designator = &quot;rus&quot;; // обозначение локали зависит от реализации,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &quot;rus&quot; подходит для VC++</div><div class="code_line">&nbsp;</div><div class="code_line">int main()</div><div class="code_line">{</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::locale loc(russian_locale_designator);</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::wstring const s &nbsp;= L&quot;русский текст&quot;;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::string const &nbsp;sa = ::narrow_string(s, loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::wstring const sw = ::wide_string(sa, loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::locale::global(loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::wcout &#60;&#60; &quot;original &nbsp;wide: &quot; &#60;&#60; s &nbsp;&#60;&#60; std::endl; </div><div class="code_line">&nbsp;&nbsp; &nbsp;std::cout &nbsp;&#60;&#60; &quot;wide -&#62; narrow: &quot; &#60;&#60; sa &#60;&#60; std::endl;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::wcout &#60;&#60; &quot;narrow -&#62; wide: &quot; &#60;&#60; sw &#60;&#60; std::endl;</div><div class="code_line">}</div></ol></div></div></div></div></div></div><br>
Возникла необходимость преобразования wstring в string. Из упомянутых здесь способов этот больше всего приглянулся. Но подскажите пожалуйста почему при первом преобразовании он из строки &quot;ddd&quot; делает строку &quot;?ddd&quot;? Причем если делать два преобразования подряд, то во второй раз символ спереди не добавляется.<br>
Я в крайнем случае могу его просто отбрасывать, но во-первых интересно знать причину, во-вторых это наводит на мысль - а не ждут ли меня еще какие сюрпризы?<br>
(способ Киллера кстати тоже прибавляет символ спереди)<br>
<br>
В этих строках у меня железно только цифры и латинские буквы, так что никакие проблемы с кодировками меня сейчас не интересуют, нужно просто самое элементарное преобразование.]]></description>
        <author>Ataru</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463832</guid>
        <pubDate>Thu, 31 Dec 2009 15:29:09 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463832</link>
        <description><![CDATA[Qraizer: Ну и мои 5 копеек:<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">std::wstring char2wchar(const std::string&amp; p_in, const std::locale&amp; p_loc)</div><div class="code_line">{</div><div class="code_line">&nbsp;try</div><div class="code_line">&nbsp;{</div><div class="code_line">&nbsp;&nbsp;const std::ctype&#60;wchar_t&#62;&amp; ctypeFacet = std::use_facet&#60;std::ctype&#60;wchar_t&#62; &#62;(p_loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;std::wstring &nbsp; &nbsp; &nbsp; &nbsp; out(p_in.length(), L&#39;{text}&#39;);</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp;std::transform(p_in.begin(), p_in.end(), out.begin(), boost::bind(boost::mem_fn(</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; static_cast&#60;wchar_t (std::ctype&#60;wchar_t&#62;::*)(char) const&#62;(&amp;std::ctype&#60;wchar_t&#62;::widen)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;ctypeFacet, _1));</div><div class="code_line">&nbsp;&nbsp;return out;</div><div class="code_line">&nbsp;}</div><div class="code_line">&nbsp;catch(...)</div><div class="code_line">&nbsp;{</div><div class="code_line">&nbsp;&nbsp;return std::wstring();</div><div class="code_line">&nbsp;} </div><div class="code_line">};</div><div class="code_line">&nbsp;</div><div class="code_line">std::string wchar2char(const std::wstring&amp; p_in, const std::locale&amp; p_loc, char def=&#39;?&#39;)</div><div class="code_line">{</div><div class="code_line">&nbsp;try</div><div class="code_line">&nbsp;{</div><div class="code_line">&nbsp;&nbsp;const std::ctype&#60;wchar_t&#62;&amp; ctypeFacet = std::use_facet&#60;std::ctype&#60;wchar_t&#62; &#62;(p_loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;std::string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;out(p_in.length(), &#39;{text}&#39;);</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp;std::transform(p_in.begin(), p_in.end(), out.begin(), boost::bind(boost::mem_fn(</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; static_cast&#60;char (std::ctype&#60;wchar_t&#62;::*)(wchar_t, char) const&#62;(&amp;std::ctype&#60;wchar_t&#62;::narrow)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;ctypeFacet, _1, def));</div><div class="code_line">&nbsp;&nbsp;return out;</div><div class="code_line">&nbsp;}</div><div class="code_line">&nbsp;catch(...)</div><div class="code_line">&nbsp;{</div><div class="code_line">&nbsp;&nbsp;return std::string();</div><div class="code_line">&nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;</div><div class="code_line">};</div></ol></div></div></div></div>]]></description>
        <author>Qraizer</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463765</guid>
        <pubDate>Thu, 31 Dec 2009 13:14:47 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463765</link>
        <description><![CDATA[Urich: <div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;std::wstring CUSCError::char2wchar( const std::string &amp; p_in, const std::locale &amp; p_loc ) const</div><div class="code_line">&nbsp;&nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;try{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;std::wstring out( p_in.length(), 0 );</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;std::string::const_iterator i = p_in.begin(), ie = p_in.end();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;std::wstring::iterator j = out.begin();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for( ; i!=ie; ++i, ++j )</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*j = std::use_facet&#60; std::ctype&#60; wchar_t &#62; &#62; ( p_loc ).widen( *i );</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return out;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}catch(...)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return std::wstring();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;} </div><div class="code_line">&nbsp;&nbsp; &nbsp;};</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::string CUSCError::wchar2char( const std::wstring &amp; p_in, const std::locale &amp; p_loc ) const</div><div class="code_line">&nbsp;&nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;typedef std::wstring::traits_type::state_type &nbsp; &nbsp; &nbsp; TsType;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;typedef std::codecvt&#60;wchar_t, char, TsType&#62; &nbsp; &nbsp; &nbsp; &nbsp; Tcvt;</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;try{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;std::string &nbsp; &nbsp; l_out( p_in.length(), char() );</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const Tcvt &amp; &nbsp; &nbsp;l_cvt = std::use_facet&#60;Tcvt&#62;(p_loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const wchar_t* &nbsp;l_from_nxt;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l_to_nxt;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TsType &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;l_state = TsType();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;l_cvt.out(l_state, &amp;p_in[0], &amp;p_in[0] + p_in.size(), l_from_nxt, &amp;l_out[0], &amp;l_out[0] + l_out.size(), l_to_nxt);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return l_out;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}catch(...)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return std::string();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;};</div></ol></div></div></div></div><br>
<br>
Не знаю на сколько это правильно но делаю так.]]></description>
        <author>Urich</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463535</guid>
        <pubDate>Wed, 30 Dec 2009 19:59:08 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463535</link>
        <description><![CDATA[vst: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=291288&view=findpost&p=2463218'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Alca &#064; <time class="tag-quote__quoted-time" datetime="2009-12-30T11:11:23+00:00">30.12.09, 11:11</time></span><div class='quote '>чтоб независимо от входной строки (Юникод или Анси) в сокетную функцию заходила Ансишная строка. </div></div><div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=291288&view=findpost&p=2463200'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Alca &#064; <time class="tag-quote__quoted-time" datetime="2009-12-30T10:46:48+00:00">30.12.09, 10:46</time></span><div class='quote '>а если юникодная строка (std::wstring) содержит иероглифы?</div></div>Надо определиться с кодировкой, в которой происходит обмен. Тогда и станет понятно, какие нужны преобразования.]]></description>
        <author>vst</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463388</guid>
        <pubDate>Wed, 30 Dec 2009 14:08:08 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463388</link>
        <description><![CDATA[Masterkent: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=291288&view=findpost&p=2463185'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Alca &#064; <time class="tag-quote__quoted-time" datetime="2009-12-30T10:29:00+00:00">30.12.09, 10:29</time></span><div class='quote '>Преобразование std::string в std::wstring туда и обратно, как делается? </div></div><br>
Стандартными средствами C++ примерно так можно:<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">#include &#60;iostream&#62;</div><div class="code_line">#include &#60;locale&#62;</div><div class="code_line">#include &#60;string&#62;</div><div class="code_line">#include &#60;vector&#62;</div><div class="code_line">&nbsp;</div><div class="code_line">std::wstring wide_string(std::string const &amp;s, std::locale const &amp;loc)</div><div class="code_line">{</div><div class="code_line">&nbsp;&nbsp; &nbsp;if (s.empty())</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return std::wstring();</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::ctype&#60;wchar_t&#62; const &amp;facet = std::use_facet&#60;std::ctype&#60;wchar_t&#62; &#62;(loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp;char const *first = s.c_str();</div><div class="code_line">&nbsp;&nbsp; &nbsp;char const *last = first + s.size();</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::vector&#60;wchar_t&#62; result(s.size());</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;facet.widen(first, last, &amp;result[0]);</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;return std::wstring(result.begin(), result.end());</div><div class="code_line">}</div><div class="code_line">&nbsp;</div><div class="code_line">std::string narrow_string(std::wstring const &amp;s, std::locale const &amp;loc, char default_char = &#39;?&#39;)</div><div class="code_line">{</div><div class="code_line">&nbsp;&nbsp; &nbsp;if (s.empty())</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return std::string();</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::ctype&#60;wchar_t&#62; const &amp;facet = std::use_facet&#60;std::ctype&#60;wchar_t&#62; &#62;(loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp;wchar_t const *first = s.c_str();</div><div class="code_line">&nbsp;&nbsp; &nbsp;wchar_t const *last = first + s.size();</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::vector&#60;char&#62; result(s.size());</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;facet.narrow(first, last, default_char, &amp;result[0]);</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;return std::string(result.begin(), result.end());</div><div class="code_line">}</div><div class="code_line">&nbsp;</div><div class="code_line">char const *russian_locale_designator = &quot;rus&quot;; // обозначение локали зависит от реализации,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &quot;rus&quot; подходит для VC++</div><div class="code_line">&nbsp;</div><div class="code_line">int main()</div><div class="code_line">{</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::locale loc(russian_locale_designator);</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::wstring const s &nbsp;= L&quot;русский текст&quot;;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::string const &nbsp;sa = ::narrow_string(s, loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::wstring const sw = ::wide_string(sa, loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::locale::global(loc);</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::wcout &#60;&#60; &quot;original &nbsp;wide: &quot; &#60;&#60; s &nbsp;&#60;&#60; std::endl; </div><div class="code_line">&nbsp;&nbsp; &nbsp;std::cout &nbsp;&#60;&#60; &quot;wide -&#62; narrow: &quot; &#60;&#60; sa &#60;&#60; std::endl;</div><div class="code_line">&nbsp;&nbsp; &nbsp;std::wcout &#60;&#60; &quot;narrow -&#62; wide: &quot; &#60;&#60; sw &#60;&#60; std::endl;</div><div class="code_line">}</div></ol></div></div></div></div>]]></description>
        <author>Masterkent</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463317</guid>
        <pubDate>Wed, 30 Dec 2009 12:47:38 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463317</link>
        <description><![CDATA[trainer: наверное.]]></description>
        <author>trainer</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463297</guid>
        <pubDate>Wed, 30 Dec 2009 12:29:20 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463297</link>
        <description><![CDATA[Alca: Там кроме бинарников я ничего нет <br>
<br>
<span class="tag-color tag-color-named" data-value="gray" style="color: gray"><span class='tag-size' data-value='7' style='font-size:7pt;'>Добавлено <time class="tag-mergetime" datetime="2009-12-30T12:35:13+00:00">30.12.09, 12:35</time></span></span><br>
Это типа http://firstcontact.svn.sourceforge.net/viewvc/firstcontact/firstcontact/trunk/GOAL/include/lexical_cast.h?revision=116 ???]]></description>
        <author>Alca</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463291</guid>
        <pubDate>Wed, 30 Dec 2009 12:20:42 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463291</link>
        <description><![CDATA[trainer: Может Флекс там и переделал чего. Было в GOAL/include/lexical_cast.h]]></description>
        <author>trainer</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463279</guid>
        <pubDate>Wed, 30 Dec 2009 12:14:13 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463279</link>
        <description><![CDATA[Alca: <div class='tag-quote'><span class='tag-quote-prefix'>Цитата</span> <div class='quote '>Вот в этом проекте: http://www.sources.ru/wiki/doku.php?id=projects:ufo есть соответствующие специализации lexical_cast</div></div><br>
в упор не вижу]]></description>
        <author>Alca</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463218</guid>
        <pubDate>Wed, 30 Dec 2009 11:11:23 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463218</link>
        <description><![CDATA[Alca: <div class='tag-quote'><span class='tag-quote-prefix'>Цитата</span> <div class='quote '>Или тебе важно соблюсти кодировку ?</div></div><br>
Скорее всего, чтоб независимо от входной строки (Юникод или Анси) в сокетную функцию заходила Ансишная строка.]]></description>
        <author>Alca</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463215</guid>
        <pubDate>Wed, 30 Dec 2009 11:10:46 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463215</link>
        <description><![CDATA[trainer: Вот в этом проекте: http://www.sources.ru/wiki/doku.php?id=projects:ufo есть соответствующие специализации lexical_cast]]></description>
        <author>trainer</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463206</guid>
        <pubDate>Wed, 30 Dec 2009 10:59:09 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463206</link>
        <description><![CDATA[KILLER: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=291288&view=findpost&p=2463200'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Alca &#064; <time class="tag-quote__quoted-time" datetime="2009-12-30T10:46:48+00:00">30.12.09, 10:46</time></span><div class='quote '>а если юникодная строка (std::wstring) содержит иероглифы? Как ее переводить в std::string? Или это бред?</div></div><br>
выдели размер равный юникодовой строке и копируй, как выше показал <br>
<br>
<span class="tag-color tag-color-named" data-value="gray" style="color: gray"><span class='tag-size' data-value='7' style='font-size:7pt;'>Добавлено <time class="tag-mergetime" datetime="2009-12-30T10:59:45+00:00">30.12.09, 10:59</time></span></span><br>
Или тебе важно соблюсти кодировку ?]]></description>
        <author>KILLER</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463200</guid>
        <pubDate>Wed, 30 Dec 2009 10:46:48 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463200</link>
        <description><![CDATA[Alca: а если юникодная строка (std::wstring) содержит иероглифы? Как ее переводить в std::string? Или это бред? <br>
<br>
Вообще мне надо конвертить строки для сокетных функций, типа такого плана:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">#if defined(UNICODE) || defined(_UNICODE)</div><div class="code_line">&nbsp;&nbsp; &nbsp;typedef std::wstring &nbsp; &nbsp; &nbsp; &nbsp;tstring; &nbsp; &nbsp; &nbsp; &nbsp; </div><div class="code_line">#else &nbsp; </div><div class="code_line">&nbsp;&nbsp; &nbsp;typedef std::string &nbsp; &nbsp; &nbsp; &nbsp; tstring; &nbsp; &nbsp; &nbsp; </div><div class="code_line">#endif &nbsp;/*_UNICODE*/</div></ol></div></div></div></div><br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">BOOL CXSocket::bConnect(const std::tstring &amp;csIp, USHORT usPort) { </div><div class="code_line">&nbsp;&nbsp; &nbsp;INT iRes = etError;</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">#if defined(UNICODE) || defined(_UNICODE)</div><div class="code_line">&nbsp;&nbsp; &nbsp;//конвертим в АНСИ</div><div class="code_line">&nbsp;&nbsp; &nbsp;//csIp -&#62; std::wstring</div><div class="code_line">#else &nbsp; </div><div class="code_line">&nbsp;&nbsp; &nbsp;//это АНСИ-строка, ничего с ней не делаем</div><div class="code_line">#endif &nbsp;/*_UNICODE*/</div><div class="code_line">&nbsp;&nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;struct sockaddr_in saSockAddr = {0}; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div><div class="code_line">&nbsp;&nbsp; &nbsp;saSockAddr.sin_family &nbsp; &nbsp; &nbsp;= AF_INET; </div><div class="code_line">&nbsp;&nbsp; &nbsp;saSockAddr.sin_addr.s_addr = inet_addr(csIp.c_str()); &nbsp; </div><div class="code_line">&nbsp;&nbsp; &nbsp;saSockAddr.sin_port &nbsp; &nbsp; &nbsp; &nbsp;= ::htons(usPort); </div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;iRes = ::connect(_m_puiSocket, (struct sockaddr *)&amp;saSockAddr, sizeof(struct sockaddr_in)); &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;/*DEBUG*/XASSERT_RET(0 == iRes, FALSE);</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;return TRUE; &nbsp; </div><div class="code_line">}</div></ol></div></div></div></div>]]></description>
        <author>Alca</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463195</guid>
        <pubDate>Wed, 30 Dec 2009 10:40:05 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463195</link>
        <description><![CDATA[KILLER: <div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;&nbsp; std::string str = &quot;12345&quot;;</div><div class="code_line">&nbsp;&nbsp; std::wstring wstr1(str.begin(), str.end());</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; std::wstring wstr2(str.length(), &#39; &#39;);</div><div class="code_line">&nbsp;&nbsp; std::copy(str.begin(), str.end(), wstr2.begin());</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; std::string str1(wstr1.begin(), wstr1.end());</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; std::string str2(wstr2.length(), L&#39; &#39;);</div><div class="code_line">&nbsp;&nbsp; std::copy(wstr2.begin(), wstr2.end(), str2.begin());</div></ol></div></div></div></div>]]></description>
        <author>KILLER</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463185</guid>
        <pubDate>Wed, 30 Dec 2009 10:29:00 +0000</pubDate>
        <title>Преобразование std::string в std::wstring туда и обратно, как делается?</title>
        <link>https://forum.sources.ru/index.php?showtopic=291288&amp;view=findpost&amp;p=2463185</link>
        <description><![CDATA[Alca: Преобразование std::string в std::wstring туда и обратно, как делается? (Юникод &lt;-&gt; Анси)]]></description>
        <author>Alca</author>
        <category>C/C++: Общие вопросы</category>
      </item>
	
      </channel>
      </rss>
	