<?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[Mit stringstream string in hex umwandeln]]></title><description><![CDATA[<p>Nabend,</p>
<p>ich möchte gerne einen String in eine Hexadezimalzahl umwandeln. Dazu habe ich gedacht, könnte man einen stringstream verwenden, was aber nicht so wirklich funktioniert.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
using namespace std;

int main()
{
	stringstream ss;
	string s;

	s = &quot;Hallo Welt&quot;;
	ss &lt;&lt; hex &lt;&lt; s;

	//Diese Ausgabe ist eine Hexadezimalzahl
	cout &lt;&lt; ss &lt;&lt; endl;

	s = ss.str();
	//Bei dieser Ausgabe kommt wieder der
	//String raus...
	cout &lt;&lt; s &lt;&lt; endl;
}
</code></pre>
<p>Wenn das nicht funktioniert, wie kann ich es sonst machen, außer selber einen Algorithmus dafür zu schreiben?<br />
Und wozu soll stringstream dann da sein?</p>
<p>Danke,<br />
Gruß Squall</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/219213/mit-stringstream-string-in-hex-umwandeln</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 14:21:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/219213.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 30 Jul 2008 20:39:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Mit stringstream string in hex umwandeln on Wed, 30 Jul 2008 20:39:10 GMT]]></title><description><![CDATA[<p>Nabend,</p>
<p>ich möchte gerne einen String in eine Hexadezimalzahl umwandeln. Dazu habe ich gedacht, könnte man einen stringstream verwenden, was aber nicht so wirklich funktioniert.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
using namespace std;

int main()
{
	stringstream ss;
	string s;

	s = &quot;Hallo Welt&quot;;
	ss &lt;&lt; hex &lt;&lt; s;

	//Diese Ausgabe ist eine Hexadezimalzahl
	cout &lt;&lt; ss &lt;&lt; endl;

	s = ss.str();
	//Bei dieser Ausgabe kommt wieder der
	//String raus...
	cout &lt;&lt; s &lt;&lt; endl;
}
</code></pre>
<p>Wenn das nicht funktioniert, wie kann ich es sonst machen, außer selber einen Algorithmus dafür zu schreiben?<br />
Und wozu soll stringstream dann da sein?</p>
<p>Danke,<br />
Gruß Squall</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1556676</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1556676</guid><dc:creator><![CDATA[Squall]]></dc:creator><pubDate>Wed, 30 Jul 2008 20:39:10 GMT</pubDate></item><item><title><![CDATA[Reply to Mit stringstream string in hex umwandeln on Wed, 30 Jul 2008 20:47:19 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int main()
{
	string s1(&quot;Hallo Welt&quot;);
	ostringstream oss;
	oss &lt;&lt; hex;
	std::copy(s1.begin(), s1.end(), ostream_iterator&lt;short&gt;(oss, &quot; &quot;));
	cout &lt;&lt; oss.str() &lt;&lt; endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1556682</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1556682</guid><dc:creator><![CDATA[Airdamn]]></dc:creator><pubDate>Wed, 30 Jul 2008 20:47:19 GMT</pubDate></item><item><title><![CDATA[Reply to Mit stringstream string in hex umwandeln on Wed, 30 Jul 2008 20:48:17 GMT]]></title><description><![CDATA[<p>...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1556683</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1556683</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Wed, 30 Jul 2008 20:48:17 GMT</pubDate></item><item><title><![CDATA[Reply to Mit stringstream string in hex umwandeln on Wed, 30 Jul 2008 21:54:27 GMT]]></title><description><![CDATA[<p>Klasse, es funktioniert.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;iterator&gt;
using namespace std;

int main()
{
	stringstream ss;
	string s;

	s = &quot;Hallo Welt!&quot;;
	ss &lt;&lt; hex;

	copy(s.begin(), s.end(), ostream_iterator&lt;short&gt;(ss, &quot; &quot;));
	cout &lt;&lt; s &lt;&lt; endl;
}
</code></pre>
<p>Also diese Iteratoren sind zur Umwandlung von Datentypen da? (Hatte ich zuvor noch nichts wirklich drüber gelesen; auch mit Templates kenne ich mich nur waage aus...)</p>
<p>Also bei dem Beispiel wird ohne den Iterator der String wieder ausgegeben. Dies passiert, weil bei der Zuweisung dieser Hex-Zahl intern die Zahl sozusagen wieder in einen String gewandelt wird, richtig?<br />
Und mit dem Iterator wird die Hex-Zahl als String geschrieben?</p>
<p>Danke,<br />
Gruß Squall</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1556720</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1556720</guid><dc:creator><![CDATA[Squall]]></dc:creator><pubDate>Wed, 30 Jul 2008 21:54:27 GMT</pubDate></item><item><title><![CDATA[Reply to Mit stringstream string in hex umwandeln on Thu, 31 Jul 2008 07:27:50 GMT]]></title><description><![CDATA[<p>Squall schrieb:</p>
<blockquote>
<p>Also diese Iteratoren sind zur Umwandlung von Datentypen da? (Hatte ich zuvor noch nichts wirklich drüber gelesen; auch mit Templates kenne ich mich nur waage aus...)</p>
<p>Also bei dem Beispiel wird ohne den Iterator der String wieder ausgegeben. Dies passiert, weil bei der Zuweisung dieser Hex-Zahl intern die Zahl sozusagen wieder in einen String gewandelt wird, richtig?<br />
Und mit dem Iterator wird die Hex-Zahl als String geschrieben?</p>
<p>Danke,<br />
Gruß Squall</p>
</blockquote>
<p>Ne, die Iteratoren sind zum Zugriff auf die Elemente eines Containers da. In diesem Fall ist String Dein Container, und begin() gibt Dir den Iterator auf das erste Element (also einen Char) zurück.<br />
Das &quot;Problem&quot; ist, dass die Streams für chars so überladen sind, dass sie sie als Zeichen interpretieren. Es werden also immer die Zeichen ausgegeben, und nicht deren ASCII-Werte.<br />
Airdamn kopierte alle Elemente im String in den Stringstream, dabei interpretiert (castet) er die einzelnen Chars aber zu short. Die shorts werden vom stringstream dann auch tatsächlich als Zahlen behandelt.</p>
<p>Noch direkter geht es übrigens so:</p>
<pre><code class="language-cpp">std::string s1(&quot;Hallo Welt&quot;);
    std::cout &lt;&lt; std::hex;
    std::copy(s1.begin(), s1.end(), std::ostream_iterator&lt;short&gt;(std::cout, &quot; &quot;));
</code></pre>
<p>Was intern abläuft kannst Du Dir in etwa so vorstellen (an die anderen: bitte nicht allzu kleinkariert sein)</p>
<pre><code class="language-cpp">std::string s1(&quot;Hallo Welt&quot;);
    std::cout &lt;&lt; std::hex;
    for(size_t n = 0; n != s1.size(); ++n)
    {
        std::cout &lt;&lt; static_cast&lt;short&gt;(s1[n]) &lt;&lt; &quot; &quot;;
    }
    std::cout &lt;&lt; '\n';
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1556790</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1556790</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Thu, 31 Jul 2008 07:27:50 GMT</pubDate></item><item><title><![CDATA[Reply to Mit stringstream string in hex umwandeln on Thu, 31 Jul 2008 08:48:29 GMT]]></title><description><![CDATA[<pre><code>+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
</code></pre>
<p>oder immernoch zu kleinkariert? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1556854</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1556854</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Thu, 31 Jul 2008 08:48:29 GMT</pubDate></item><item><title><![CDATA[Reply to Mit stringstream string in hex umwandeln on Thu, 31 Jul 2008 13:11:48 GMT]]></title><description><![CDATA[<p>Vielen Dank für die Erläuterung.<br />
Nun bin ich wunschlos glücklich! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Gruß<br />
Squall</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1557031</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1557031</guid><dc:creator><![CDATA[Squall]]></dc:creator><pubDate>Thu, 31 Jul 2008 13:11:48 GMT</pubDate></item></channel></rss>