<?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[einen INT byteweise vertauschen]]></title><description><![CDATA[<p>Hallo,<br />
ich habe folgendes Problem,<br />
ich muss aus einer siemens Steuerung Daten auslesen. Dazu stellt Siemens ne dll bereit. Als Ergebnis bekomme ich einen Zeiger auf das anfang der Zahl so z.B</p>
<pre><code>int16  *value_array[1]; // int16 = SHORT INT
s7_get_cycl_read_ind((void *)0,&amp;result,&amp;var_length, value_array);
int16 a = *value_array[0];
</code></pre>
<p>Nun zum Problem. In der Steuerung sind die Bytes vertauscht.<br />
zB. ist 38 als</p>
<pre><code>0000_0000_0010_0110
</code></pre>
<p>dargestellt. Unter Windows bedeutet das natürlich</p>
<pre><code>0010_0110_0000_0000
</code></pre>
<p>also 9728 <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Hat da jemand ne Idee, wie ich die Bytes tauschen könnte?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/124216/einen-int-byteweise-vertauschen</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 21:55:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/124216.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 24 Oct 2005 21:25:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Mon, 24 Oct 2005 21:25:17 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich habe folgendes Problem,<br />
ich muss aus einer siemens Steuerung Daten auslesen. Dazu stellt Siemens ne dll bereit. Als Ergebnis bekomme ich einen Zeiger auf das anfang der Zahl so z.B</p>
<pre><code>int16  *value_array[1]; // int16 = SHORT INT
s7_get_cycl_read_ind((void *)0,&amp;result,&amp;var_length, value_array);
int16 a = *value_array[0];
</code></pre>
<p>Nun zum Problem. In der Steuerung sind die Bytes vertauscht.<br />
zB. ist 38 als</p>
<pre><code>0000_0000_0010_0110
</code></pre>
<p>dargestellt. Unter Windows bedeutet das natürlich</p>
<pre><code>0010_0110_0000_0000
</code></pre>
<p>also 9728 <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Hat da jemand ne Idee, wie ich die Bytes tauschen könnte?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/900451</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900451</guid><dc:creator><![CDATA[Roman007]]></dc:creator><pubDate>Mon, 24 Oct 2005 21:25:17 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Mon, 24 Oct 2005 21:28:26 GMT]]></title><description><![CDATA[<p>kleines Missgeschick von mir. Die beiden Binärdarstellungen sollten genau umgekehrt sein. Sorry</p>
<p>Roman</p>
]]></description><link>https://www.c-plusplus.net/forum/post/900453</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900453</guid><dc:creator><![CDATA[Roman007]]></dc:creator><pubDate>Mon, 24 Oct 2005 21:28:26 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Mon, 24 Oct 2005 21:44:35 GMT]]></title><description><![CDATA[<p>Es gibt viele Möglichkeiten. Eine wäre:</p>
<pre><code class="language-cpp">int16 val = ...; 
val = (val &lt;&lt; 8)|(val &gt;&gt; 8);
</code></pre>
<p>Du könntest auch google nach little und big endian befragen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/900457</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900457</guid><dc:creator><![CDATA[Ben04]]></dc:creator><pubDate>Mon, 24 Oct 2005 21:44:35 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Mon, 24 Oct 2005 21:47:23 GMT]]></title><description><![CDATA[<p>Dein Code ist keine so gute Idee, weil val vorzeichenbehaftet ist. Der Ausdruck in der zweiten Zeile sollte lauten:</p>
<pre><code class="language-cpp">(val &lt;&lt; 8) | ((val &gt;&gt; 8) &amp; 0xff)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/900460</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900460</guid><dc:creator><![CDATA[Ringding]]></dc:creator><pubDate>Mon, 24 Oct 2005 21:47:23 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Tue, 25 Oct 2005 06:51:45 GMT]]></title><description><![CDATA[<p>Du könntest die Funktionen (oder je nach Impl. Makros) htons bzw. ntohs benutzen (<strong>H</strong>ost- <strong>to</strong> <strong>N</strong>etwork-Byteorder - <strong>s</strong>hort], bzw. umgekehrt) benutzen, die diese Umwandlung je nach Plattform richtig für dich machen. Allerdings gibt's die in reinem C++ nicht, sondern werden Dir unter Windows z.B. von der Socketbibliothek zur Verfügung gestellt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/900566</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900566</guid><dc:creator><![CDATA[7H3 N4C3R]]></dc:creator><pubDate>Tue, 25 Oct 2005 06:51:45 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Tue, 25 Oct 2005 20:56:00 GMT]]></title><description><![CDATA[<p>Roman007 schrieb:</p>
<blockquote>
<p>Hat da jemand ne Idee, wie ich die Bytes tauschen könnte?</p>
</blockquote>
<pre><code class="language-cpp">#include &lt;algorithm&gt;

    int value = 38;  // oder short oder long oder ...
    std::reverse( reinterpret_cast&lt; char* &gt;( &amp;value ), reinterpret_cast&lt; char* &gt;( &amp;value + 1 ) );
</code></pre>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/900723</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900723</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 25 Oct 2005 20:56:00 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Tue, 25 Oct 2005 21:14:46 GMT]]></title><description><![CDATA[<p>oder so</p>
<pre><code class="language-cpp">#include &lt;algorithm&gt;

int value = 0x1133;  // oder short oder long oder ...
std::swap(*(reinterpret_cast&lt;char*&gt;(&amp;value)), *(reinterpret_cast&lt;char*&gt;(&amp;value) + 1));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/900733</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900733</guid><dc:creator><![CDATA[Redhead]]></dc:creator><pubDate>Tue, 25 Oct 2005 21:14:46 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Tue, 25 Oct 2005 21:24:08 GMT]]></title><description><![CDATA[<p>Redhead schrieb:</p>
<blockquote>
<p>oder so</p>
<pre><code class="language-cpp">#include &lt;algorithm&gt;

int value = 0x1133;  // oder short oder long oder ...
std::swap(*(reinterpret_cast&lt;char*&gt;(&amp;value)), *(reinterpret_cast&lt;char*&gt;(&amp;value) + 1));
</code></pre>
</blockquote>
<p>hast Du das getestet? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f576.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--sunglasses"
      title=":sunglasses:"
      alt="🕶"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/900740</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900740</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 25 Oct 2005 21:24:08 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Tue, 25 Oct 2005 21:26:28 GMT]]></title><description><![CDATA[<p>jep(VC6), es macht<br />
0x1133 =&gt; 0x3311</p>
<p>hoffe wir meinen/machen das Gleiche. <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/900742</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900742</guid><dc:creator><![CDATA[Redhead]]></dc:creator><pubDate>Tue, 25 Oct 2005 21:26:28 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Tue, 25 Oct 2005 21:34:37 GMT]]></title><description><![CDATA[<p>Sehe ein das wir nicht GANZ das Gleiche tun. Hätte vielleicht vorher die &quot;Original&quot;-Aufgabe<br />
lesen sollen. <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="😃"
    /> <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="😃"
    /> <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/900745</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900745</guid><dc:creator><![CDATA[Redhead]]></dc:creator><pubDate>Tue, 25 Oct 2005 21:34:37 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Tue, 25 Oct 2005 21:45:13 GMT]]></title><description><![CDATA[<p>Redhead schrieb:</p>
<blockquote>
<p>jep(VC6), es macht<br />
0x1133 =&gt; 0x3311</p>
<p>hoffe wir meinen/machen das Gleiche. <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>
</blockquote>
<p>vielleicht nicht.<br />
Ich hatte den OP so versstanden, die Reihenfolge aller (!) Bytes eines int-Wertes zu vertauschen. Dein Vorschlag vertauscht die ersten beiden Bytes im Memory unabhängig davon, aus wieviel Bytes der Ziel-Wert besteht (int i.A. 4 Bytes). Alle andere Bytes bleiben stehen.<br />
Wenn Du einen int (von 4 Bytes) vom Wert 0x1133 hast, so würde ich beim Vertauschen 0x33110000 erwarten.<br />
Bei int16-Werten funkt's natürlich auch mit swap.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/900749</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900749</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 25 Oct 2005 21:45:13 GMT</pubDate></item><item><title><![CDATA[Reply to einen INT byteweise vertauschen on Tue, 25 Oct 2005 22:18:52 GMT]]></title><description><![CDATA[<p>danke an alle. <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 />
ich habe seit gestern keine Möglichkeit vorbeizuschauen.<br />
Es hat sich was getan, ich wusste gar nicht, dass es so einfach ist, die Bytes zu tauschen. Ich habe gleich die erste Methode gewählt, und die funzt schon.<br />
Das es bei jedem Datentyp läuft hab ich die Funktionen überladen</p>
<pre><code>void endian_swap(unsigned short&amp; x) { x = (x&gt;&gt;8)| (x&lt;&lt;8); &amp;0x00FF } 
void endian_swap(unsigned int&amp; x) { x = (x&gt;&gt;24) | ((x&lt;&lt;8) &amp; 0x00FF0000) 
                                                | ((x&gt;&gt;8) &amp; 0x0000FF00) 
                                                | (x&lt;&lt;24); } 
inline void endian_swap(unsigned int64&amp; x) { x = (x&gt;&gt;56) 
                                            | ((x&lt;&lt;40) &amp; 0x00FF000000000000) 
                                            | ((x&lt;&lt;24) &amp; 0x0000FF0000000000) 
                                            | ((x&lt;&lt;8)  &amp; 0x000000FF00000000) 
                                            | ((x&gt;&gt;8)  &amp; 0x00000000FF000000) 
                                            | ((x&gt;&gt;24) &amp; 0x0000000000FF0000) 
                                            | ((x&gt;&gt;40) &amp; 0x000000000000FF00) 
                                            | (x&lt;&lt;56); }
</code></pre>
<p>Euere Lösungenen sehen aber einfacher aus, muss es ausprobieren</p>
<p>Danke nochmal<br />
Roman</p>
]]></description><link>https://www.c-plusplus.net/forum/post/900763</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/900763</guid><dc:creator><![CDATA[Roman007]]></dc:creator><pubDate>Tue, 25 Oct 2005 22:18:52 GMT</pubDate></item></channel></rss>