<?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[struct in string kopieren]]></title><description><![CDATA[<p>Also...</p>
<p>ich habe ein Stuct :</p>
<pre><code class="language-cpp">struct MSGHeader
{
       unsigned int Version;
       unsigned int Type;
       unsigned int Len;
};
</code></pre>
<p>Wie man sieht ist das ein Header für eine Nachricht, die Nachricht ist ein String. so ich will jetzt dieses Struct (bzw. die bytes dieses Structs) an den Anfang des strings packe (an der gegenstelle wird das ganze dann wieder andersrum gemacht).<br />
Aber ich bekomme es einfach nicht hin:<br />
Stringstream nimmt mein struct nicht weil es ja ein eigener Typ ist.... wie stelle ich das jetzt am besten an?<br />
Bin für jede Hilfe dankbar</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/181793/struct-in-string-kopieren</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 11:05:07 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/181793.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 17 May 2007 13:33:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 13:33:31 GMT]]></title><description><![CDATA[<p>Also...</p>
<p>ich habe ein Stuct :</p>
<pre><code class="language-cpp">struct MSGHeader
{
       unsigned int Version;
       unsigned int Type;
       unsigned int Len;
};
</code></pre>
<p>Wie man sieht ist das ein Header für eine Nachricht, die Nachricht ist ein String. so ich will jetzt dieses Struct (bzw. die bytes dieses Structs) an den Anfang des strings packe (an der gegenstelle wird das ganze dann wieder andersrum gemacht).<br />
Aber ich bekomme es einfach nicht hin:<br />
Stringstream nimmt mein struct nicht weil es ja ein eigener Typ ist.... wie stelle ich das jetzt am besten an?<br />
Bin für jede Hilfe dankbar</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1286789</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1286789</guid><dc:creator><![CDATA[download123]]></dc:creator><pubDate>Thu, 17 May 2007 13:33:31 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 14:08:10 GMT]]></title><description><![CDATA[<p>Hi,</p>
<pre><code class="language-cpp">string ToString(int val)
{
  ostringstream ss;
  ss &lt;&lt; val;
  return ss.str();
}

struct MSGHeader
{
       unsigned int Version;
       unsigned int Type;
       unsigned int Len;
};

MSGHeader&amp; operator&gt;&gt; (const MSGHeader&amp; header, string&amp; str)
{
   str = ToString(header.Version) + &quot; &quot; + ToString(header.Type) + &quot; &quot; + ToString(header.Len);
   return header;
}

// ...
MSGHeader h; // fill h
h &gt;&gt; mystring;
</code></pre>
<p>Hoffe mal das meintest du ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1286808</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1286808</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 17 May 2007 14:08:10 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 14:14:20 GMT]]></title><description><![CDATA[<p>ähm... jein.. es ist ne möglcihkeit.</p>
<p>Allerdings hab ich ja indirekt das gemacht was ich nicht ganz wollte, die zahlen zu phrasen und als zahlen in den string zu schreiben..</p>
<p>was ich eigentlich dachte wäre das struct als datenmasse zu sehen (bytes) die einfach in dem string abgelegt werden. Sprich quasi unverändert...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1286809</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1286809</guid><dc:creator><![CDATA[download123]]></dc:creator><pubDate>Thu, 17 May 2007 14:14:20 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 18:47:09 GMT]]></title><description><![CDATA[<p>so vielleicht:</p>
<pre><code class="language-cpp">std::string s(reinterpret_cast&lt;char*&gt;(&amp;msgheader), sizeof(msgheader));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1286966</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1286966</guid><dc:creator><![CDATA[codec]]></dc:creator><pubDate>Thu, 17 May 2007 18:47:09 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 20:32:40 GMT]]></title><description><![CDATA[<p>codec schrieb:</p>
<blockquote>
<p>so vielleicht:</p>
<pre><code class="language-cpp">std::string s(reinterpret_cast&lt;char*&gt;(&amp;msgheader), sizeof(msgheader));
</code></pre>
</blockquote>
<p>sieht sehr gut aus, funktzt auch.<br />
eine Frage bleibt allerdings ... wie sieht das andersrum aus?<br />
also von string zurück zum struct,<br />
ich habs probiert, allerdings probleme damit</p>
<pre><code class="language-cpp">//gegeben ist
//string *Msg mit dem umgewandelten stuckt am anfang und danach dem inhalt

MSGHeader header(reinterpret_cast&lt;MSGHeader&gt;(Msg-&gt;substr(0,sizeof(header))), sizeof(header));
</code></pre>
<p>aber es klappt nicht</p>
<pre><code>error: invalid cast from type ‘std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;’ to type ‘MSGHeader’
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1287039</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1287039</guid><dc:creator><![CDATA[download123]]></dc:creator><pubDate>Thu, 17 May 2007 20:32:40 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 20:40:01 GMT]]></title><description><![CDATA[<p>evt. MSGHeader* pHeader = reinterpret_cast&lt;MSGHeader*&gt;(Msg-&gt;c_str()); ... aber eher nicht ... was du da machst ist insgesamt nicht sehr sinnig ... sry <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="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1287045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1287045</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Thu, 17 May 2007 20:40:01 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 20:40:33 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">memcpy(&amp;header, s.data(), s.length());
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1287047</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1287047</guid><dc:creator><![CDATA[.....]]></dc:creator><pubDate>Thu, 17 May 2007 20:40:33 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 20:46:03 GMT]]></title><description><![CDATA[<p>(D)Evil schrieb:</p>
<blockquote>
<p>evt. MSGHeader* pHeader = reinterpret_cast&lt;MSGHeader*&gt;(Msg-&gt;c_str()); ... aber eher nicht ... was du da machst ist insgesamt nicht sehr sinnig ... sry <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="😉"
    /></p>
</blockquote>
<p>von außen gesehen nicht...</p>
<p>wenn man aber über seine sockets nur strings übertragen kann.. dann schon <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="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1287051</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1287051</guid><dc:creator><![CDATA[download123]]></dc:creator><pubDate>Thu, 17 May 2007 20:46:03 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 21:09:08 GMT]]></title><description><![CDATA[<p>Über einen Socket kannst du nur char* übertragen ... da ist ein unterschied ... ein großer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1287068</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1287068</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Thu, 17 May 2007 21:09:08 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 21:12:51 GMT]]></title><description><![CDATA[<p>download123 schrieb:</p>
<blockquote>
<p>(D)Evil schrieb:</p>
<blockquote>
<p>evt. MSGHeader* pHeader = reinterpret_cast&lt;MSGHeader*&gt;(Msg-&gt;c_str()); ... aber eher nicht ... was du da machst ist insgesamt nicht sehr sinnig ... sry <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="😉"
    /></p>
</blockquote>
<p>von außen gesehen nicht...</p>
<p>wenn man aber über seine sockets nur strings übertragen kann.. dann schon <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="😉"
    /></p>
</blockquote>
<p>Gerade dann würde ich mich nicht abhängig von Compilerversionen und internen Speicherrepräsentationen machen, sondern ein vernünftiges Protokoll definieren ... oder ein bestehendes einsetzen.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1287071</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1287071</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 17 May 2007 21:12:51 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 21:18:07 GMT]]></title><description><![CDATA[<p>(D)Evil schrieb:</p>
<blockquote>
<p>Über einen Socket kannst du nur char* übertragen ... da ist ein unterschied ... ein großer.</p>
</blockquote>
<p>welchen meinst du?</p>
<p>Simon2 schrieb:</p>
<blockquote>
<p>Gerade dann würde ich mich nicht abhängig von Compilerversionen und internen Speicherrepräsentationen machen, sondern ein vernünftiges Protokoll definieren ... oder ein bestehendes einsetzen.</p>
<p>Gruß,</p>
<p>Simon2.</p>
</blockquote>
<p>Gib doch mal ein beispiel was du mit vernünftiges Protokoll meinst?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1287079</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1287079</guid><dc:creator><![CDATA[download123]]></dc:creator><pubDate>Thu, 17 May 2007 21:18:07 GMT</pubDate></item><item><title><![CDATA[Reply to struct in string kopieren on Thu, 17 May 2007 21:46:28 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>naja das einfachste hat bereits KasF vorgeschlagen. Es fordert allerdings festgelegte Reihenfolge und Typen der Parameter und bietet keinen &quot;Kompatibilitätsschutz&quot;, aber für eine einache Anwendung reicht das erstmal.<br />
Ansonsten ist bis XML (will das nicht als ideal darstellen, ist aber schon ein recht mächtiges Protokoll) alles möglich.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1287090</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1287090</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 17 May 2007 21:46:28 GMT</pubDate></item></channel></rss>