<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[wstring2string]]></title><description><![CDATA[<p>Hallo Forum,</p>
<p>wie wandelt man am besten einen wstring in einen std::string um? (Visual Studio 6)<br />
Ich suche gewissermassen das Gegenstück zu dem Code im zweiten Posting dieses Threads:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-p-is-1197611.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-p-is-1197611.html</a></p>
<p>Im Internet habe ich diese Codeschnipsel gefunden:</p>
<pre><code class="language-cpp">// http://www.c-plusplus.net/forum/viewtopic-var-p-is-285374.html#285374
char Narrow[6] = &quot;Hallo&quot;;
wchar_t Widen[6];
_USE(cout.getloc(), ctype&lt;wchar_t&gt; ).widen(Narrow, Narrow+6, Widen); 

void wstring2string(std::string &amp;dest,const std::wstring &amp;src) {
    dest.resize(src.size());
    for (uint32_t i=0; i&lt;src.size(); i++)
        dest[i] = src[i] &lt; 256 ? src[i] : ' ';
}
</code></pre>
<p>Die wollte ich aber nicht unbeding benutzen...</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/182984/wstring2string</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 01:58:14 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/182984.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 31 May 2007 10:48:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 10:48:52 GMT]]></title><description><![CDATA[<p>Hallo Forum,</p>
<p>wie wandelt man am besten einen wstring in einen std::string um? (Visual Studio 6)<br />
Ich suche gewissermassen das Gegenstück zu dem Code im zweiten Posting dieses Threads:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-p-is-1197611.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-p-is-1197611.html</a></p>
<p>Im Internet habe ich diese Codeschnipsel gefunden:</p>
<pre><code class="language-cpp">// http://www.c-plusplus.net/forum/viewtopic-var-p-is-285374.html#285374
char Narrow[6] = &quot;Hallo&quot;;
wchar_t Widen[6];
_USE(cout.getloc(), ctype&lt;wchar_t&gt; ).widen(Narrow, Narrow+6, Widen); 

void wstring2string(std::string &amp;dest,const std::wstring &amp;src) {
    dest.resize(src.size());
    for (uint32_t i=0; i&lt;src.size(); i++)
        dest[i] = src[i] &lt; 256 ? src[i] : ' ';
}
</code></pre>
<p>Die wollte ich aber nicht unbeding benutzen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295815</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295815</guid><dc:creator><![CDATA[Jakata]]></dc:creator><pubDate>Thu, 31 May 2007 10:48:52 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 11:02:51 GMT]]></title><description><![CDATA[<p>Ich sehe gerade der erste Code aus der FAQ macht aus char ein wchar_t.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295821</guid><dc:creator><![CDATA[Jakata]]></dc:creator><pubDate>Thu, 31 May 2007 11:02:51 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 11:19:35 GMT]]></title><description><![CDATA[<p>Hi!</p>
<p>Das geht so:</p>
<pre><code class="language-cpp">wchar_t Widen[6] = L&quot;Hallo&quot;;
char Narrow[6];
use_facet&lt;ctype&lt;wchar_t&gt; &gt; (cout.getloc()).narrow(Widen, Widen+6, '?', Narrow);
// oder wem das VC-Makro gefällt:
_USE(cout.getloc(), ctype&lt;wchar_t&gt;).narrow(Widen, Widen+6, '?', Narrow);
</code></pre>
<p>Der dritte Parameter gibt an, welches Zeichen eingesetzt werden soll, wenn es keinen entsprechenden char geben sollte bei der Umwandlung.</p>
<p>Greetz</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295827</guid><dc:creator><![CDATA[Vellas]]></dc:creator><pubDate>Thu, 31 May 2007 11:19:35 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 11:32:28 GMT]]></title><description><![CDATA[<p>Erstmal vielen Dank für die Mühe. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Weißt Du welchen Header ich noch einbinden muß? In Google scheint die Meldung noch nicht vorgekommen zu sein:<br />
<a href="http://www.google.de/search?hl=de&amp;client=firefox-a&amp;rls=org.mozilla%3Ade%3Aofficial&amp;hs=vK3&amp;q=%22%27use_facet%27+%3A+undeclared+identifier%22&amp;btnG=Suche&amp;meta=" rel="nofollow">http://www.google.de/search?hl=de&amp;client=firefox-a&amp;rls=org.mozilla%3Ade%3Aofficial&amp;hs=vK3&amp;q=&quot;'use_facet'+%3A+undeclared+identifier&quot;&amp;btnG=Suche&amp;meta=</a></p>
<pre><code class="language-cpp">void wstring2string(std::string &amp;dest,const std::wstring &amp;src) {

	const wchar_t *Widen = src.c_str();
	char *Narrow = new char[src.length()+1];

	use_facet&lt;ctype&lt;wchar_t&gt; &gt; (cout.getloc()).narrow(*Widen, *Widen + src.length(), '?', Narrow);
	//_USE(cout.getloc(), ctype&lt;wchar_t&gt;).narrow(*Widen, *Widen + src.length(), '?', Narrow);
}

// Die Fehler
//error C2065: 'use_facet' : undeclared identifier
//error C2065: 'ctype' : undeclared identifier
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1295838</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295838</guid><dc:creator><![CDATA[Jakata]]></dc:creator><pubDate>Thu, 31 May 2007 11:32:28 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 11:40:38 GMT]]></title><description><![CDATA[<p>Siehe: <a href="http://msdn2.microsoft.com/en-us/library/e3y8541c(VS.80).aspx" rel="nofollow">http://msdn2.microsoft.com/en-us/library/e3y8541c(VS.80).aspx</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295848</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295848</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Thu, 31 May 2007 11:40:38 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 11:53:09 GMT]]></title><description><![CDATA[<p>Ich habe den Header locale.h eingebunden. Die Fehlermeldung ist jetzt das 3 Argumente antatt 1 erwartet wird. Soweit ich zählen kann übergebe ich aber 3 Argumente. Was ist der Fehler?</p>
<pre><code class="language-cpp">#include &lt;locale&gt;

void wstring2string(std::string &amp;dest,const std::wstring &amp;src) {

	const wchar_t *Widen = src.c_str();
	char *Narrow = new char[src.length()+1];

	// Diese Zeile mird angemeckert:
	std::use_facet&lt;std::ctype&lt;wchar_t&gt; &gt; (std::cout.getloc()).narrow(*Widen, *Widen + src.length(), '?', Narrow);
	//_USE(cout.getloc(), ctype&lt;wchar_t&gt;).narrow(*Widen, *Widen + src.length(), '?', Narrow);
}

// error C2780: 'const _F &amp;__cdecl std::use_facet(const class std::locale &amp;,const _F *,bool)'
// : expects 3 arguments - 1 provided
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1295861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295861</guid><dc:creator><![CDATA[Jakata]]></dc:creator><pubDate>Thu, 31 May 2007 11:53:09 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 12:54:14 GMT]]></title><description><![CDATA[<p>Hi!</p>
<p>Hab da vorhin mal 2 Funktionen erstellt, die sollten funktionieren:</p>
<pre><code class="language-cpp">std::string wstring2string(std::wstring const &amp;str, std::locale const &amp;loc=std::locale()) {
  size_t len = str.size();
  char *tmp = new char[len+1];
  std::use_facet&lt;std::ctype&lt;wchar_t&gt; &gt;(loc).narrow(str.data(), str.data()+len, '?', tmp);
  tmp[len] = 0;
  std::string res(tmp);
  delete []tmp;
  return res;
}

std::wstring string2wstring(std::string const &amp;str, std::locale const &amp;loc=std::locale()) {
  size_t len = str.size();
  wchar_t *tmp = new wchar_t[len+1];
  std::use_facet&lt;std::ctype&lt;wchar_t&gt; &gt;(loc).widen(str.data(), str.data()+len, tmp);
  tmp[len] = 0;
  std::wstring res(tmp);
  delete []tmp;
  return res;
}
</code></pre>
<p>Wahrscheinlich hast du kein &quot;using namespace std;&quot; bei dir im Programm, weshalb use_facet nicht gefunden wird, liegt nämlich auch (wie eigentlich alles in C++) im Namespace std.<br />
Aufrufen kannst du das ganze von mir etwa so:</p>
<pre><code class="language-cpp">std::string str = wstring2string(L&quot;Hallo&quot;, std::cout.getloc()); // oder ohne zweiten Parameter
std::wstring wstr = string2wstring(str, std::cout.getloc()); // oder ohne zweiten Parameter
std::cout &lt;&lt; str &lt;&lt; std::endl;
std::wcout &lt;&lt; wstr &lt;&lt; std::endl;
</code></pre>
<p>Greetz</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295911</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295911</guid><dc:creator><![CDATA[Vellas]]></dc:creator><pubDate>Thu, 31 May 2007 12:54:14 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 13:04:06 GMT]]></title><description><![CDATA[<p>Man sollte übrigens noch erwähnen, dass die StdLib selbst keinerlei Konvertierung von einem Zeichensatz in einen anderen vornimmt.<br />
Wer das braucht benötigt eine zusätzliche Bibliothek (z.B. von Dinkumware).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295928</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295928</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Thu, 31 May 2007 13:04:06 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 13:24:26 GMT]]></title><description><![CDATA[<p>Hallo Vellas,</p>
<p>erstmal vielen Dank das Du Dir Mühe gemacht hast <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Der Fehler ist aber noch immer vorhanden: Ich erstelle ein neues VS6 Kommandozeilen Projekt und kopiere diesen Source rein:</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &lt;locale&gt;
using namespace std;

std::string wstring2string(std::wstring const &amp;str, std::locale const &amp;loc=std::locale()) {
  size_t len = str.size();
  char *tmp = new char[len+1];
  std::use_facet&lt;std::ctype&lt;wchar_t&gt; &gt;(loc).narrow(str.data(), str.data()+len, '?', tmp);
  tmp[len] = 0;
  std::string res(tmp);
  delete []tmp;
  return res;
}

int main(int argc, char* argv[]) {
	printf(&quot;Hello World!\n&quot;);
	return 0;
}
</code></pre>
<p>Als Fehlermeldung bekomme ich:</p>
<blockquote>
<p>error C2780: 'const _F &amp;__cdecl std::use_facet(const class std::locale &amp;,const _F *,bool)' : expects 3 arguments - 1 provided<br />
c:\program files\microsoft visual studio\vc98\include\xlocale(157) : see declaration of 'use_facet'<br />
error C2228: left of '.narrow' must have class/struct/union type</p>
</blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13970">@Lolz</a>: Eine extra DLL wollte ich nicht einbinden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295944</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295944</guid><dc:creator><![CDATA[Jakata]]></dc:creator><pubDate>Thu, 31 May 2007 13:24:26 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 13:29:20 GMT]]></title><description><![CDATA[<p>Das scheint ein Problem deines VC6 zu sein. Bei dir will der Konstruktor von use_facet 3 Parameter. Laut Standard (und in deinem Code) wird aber nur ein Parameter benötigt.<br />
Kannst du nicht einen aktuelleren Compiler nehmen (VC2005 oder so)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295949</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295949</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Thu, 31 May 2007 13:29:20 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 13:30:52 GMT]]></title><description><![CDATA[<p>Ich glaub das liegt am VS6, schau dir mal die Dokumentation zu std::use_facet an (in der msdn version die beim vs6 dabei war). Scheinbar muss man in dieser Version noch zusätzliche Parameter mitangeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295951</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295951</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Thu, 31 May 2007 13:30:52 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 13:37:20 GMT]]></title><description><![CDATA[<p>Ich habe hier was dazu gefunden. Ist aber französisch. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
<a href="http://www.developpez.net/forums/archive/index.php/t-19922.html" rel="nofollow">http://www.developpez.net/forums/archive/index.php/t-19922.html</a><br />
Ich interpretiere das mal so, dass das hier gehen müsste</p>
<pre><code class="language-cpp">std::use_facet&lt;std::ctype&lt;wchar_t&gt; &gt;(loc, 0, true).narrow(str.data(), str.data()+len, '?', tmp);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1295953</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295953</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Thu, 31 May 2007 13:37:20 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 13:42:59 GMT]]></title><description><![CDATA[<p>Naja, das VS6 benutzen hier noch alle. Da bleibe ich erstmal bei diesem Projektformat.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/1387">@Braunstein</a>: Das hat genau hingehauen. Vielen Dank <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295970</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295970</guid><dc:creator><![CDATA[Jakata]]></dc:creator><pubDate>Thu, 31 May 2007 13:42:59 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 13:52:07 GMT]]></title><description><![CDATA[<p>Alle springen von der Brücke. Die springst hinterher.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1295980</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1295980</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Thu, 31 May 2007 13:52:07 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 14:23:34 GMT]]></title><description><![CDATA[<p>Mag sein das VS6 veraltet ist, aber so erspare ich mir Ärger in der Firma. Wie man an diesem Fall sieht sind nicht nur die Projektdateien anders, sondern auch der C++ Code. Die Kollegen würden <strong>mich</strong> von der Brücke stoßen wenn ich für VS6 nicht kompilierbaren Code verteilen würde. Und als Werkstudent habe ich eh keinen grossen Einfluß um einen Umstieg auf .Net Studio zu erzwingen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1296029</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1296029</guid><dc:creator><![CDATA[Jakata]]></dc:creator><pubDate>Thu, 31 May 2007 14:23:34 GMT</pubDate></item><item><title><![CDATA[Reply to wstring2string on Thu, 31 May 2007 14:28:26 GMT]]></title><description><![CDATA[<p>In dem Fall würd ich aber doch eher zum _USE Makro tendieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1296035</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1296035</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Thu, 31 May 2007 14:28:26 GMT</pubDate></item></channel></rss>