<?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[Umwandlung von JAVA double in C++ double]]></title><description><![CDATA[<p>Hallo,<br />
folgendes Szenario: Ich habe in C++ einen TCP Server laufen und in JAVA einen TCP client. Jetzt schickt der JAVA client dem server Daten in Form von integers und doubles. In C++ kommen diese jedoch erstmal als purer Spreicher an:</p>
<pre><code>uint8* buffer;//z.B. 8 byte lang für ein double
</code></pre>
<p>Wenn ich jetzt folgendes mache, kommen jedoch falsche Zahlen heraus:</p>
<pre><code>int32 i;
std::memcpy(&amp;i, buffer, 4);
</code></pre>
<p>Mittlerweile habe ich das für integer unsauber gelöst:</p>
<pre><code>int32 bitsToInt(uint8* bytes) {
	int32 i;
	if (bytes[0] &lt; 128) {
		i = bytes[0];
		i = i &lt;&lt; 8;
		i += bytes[1];
		i = i &lt;&lt; 8;
		i += bytes[2];
		i = i &lt;&lt; 8;
		i += bytes[3];
	}
	else {
		i = bytes[0]-128;
		i = i &lt;&lt; 8;
		i += bytes[1];
		i = i &lt;&lt; 8;
		i += bytes[2];
		i = i &lt;&lt; 8;
		i += bytes[3];
		i -= 2147483648;
	}

	return i;
}
</code></pre>
<p>Für IEEE 754 double bekomme ich das so jedoch irgendwie nicht hin und es ist mir auch zu blöd.</p>
<p>Die Frage ist also wieso funktioniert memcpy oder ein einfacher cast nicht?</p>
<p>Ich freue mich schon auf eure Antworten.<br />
msg lonol15</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/337357/umwandlung-von-java-double-in-c-double</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 06:12:50 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/337357.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 27 Mar 2016 15:09:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Sun, 27 Mar 2016 15:09:30 GMT]]></title><description><![CDATA[<p>Hallo,<br />
folgendes Szenario: Ich habe in C++ einen TCP Server laufen und in JAVA einen TCP client. Jetzt schickt der JAVA client dem server Daten in Form von integers und doubles. In C++ kommen diese jedoch erstmal als purer Spreicher an:</p>
<pre><code>uint8* buffer;//z.B. 8 byte lang für ein double
</code></pre>
<p>Wenn ich jetzt folgendes mache, kommen jedoch falsche Zahlen heraus:</p>
<pre><code>int32 i;
std::memcpy(&amp;i, buffer, 4);
</code></pre>
<p>Mittlerweile habe ich das für integer unsauber gelöst:</p>
<pre><code>int32 bitsToInt(uint8* bytes) {
	int32 i;
	if (bytes[0] &lt; 128) {
		i = bytes[0];
		i = i &lt;&lt; 8;
		i += bytes[1];
		i = i &lt;&lt; 8;
		i += bytes[2];
		i = i &lt;&lt; 8;
		i += bytes[3];
	}
	else {
		i = bytes[0]-128;
		i = i &lt;&lt; 8;
		i += bytes[1];
		i = i &lt;&lt; 8;
		i += bytes[2];
		i = i &lt;&lt; 8;
		i += bytes[3];
		i -= 2147483648;
	}

	return i;
}
</code></pre>
<p>Für IEEE 754 double bekomme ich das so jedoch irgendwie nicht hin und es ist mir auch zu blöd.</p>
<p>Die Frage ist also wieso funktioniert memcpy oder ein einfacher cast nicht?</p>
<p>Ich freue mich schon auf eure Antworten.<br />
msg lonol15</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491609</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491609</guid><dc:creator><![CDATA[lonol15]]></dc:creator><pubDate>Sun, 27 Mar 2016 15:09:30 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Sun, 27 Mar 2016 16:31:17 GMT]]></title><description><![CDATA[<p>Du solltest lieber zu einem anderen Format zurückgreifen, wie z.B JSON oder XML, als reine Bytes zu verwenden, wenn du zwischen Java und C++ kommunizierst. Das bereitet nämlich weniger Kopfschmerzen.</p>
<p>Falls es anders nicht zu lösen ist, bräuchte man noch deinen JAVA Code.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491613</guid><dc:creator><![CDATA[froheostern]]></dc:creator><pubDate>Sun, 27 Mar 2016 16:31:17 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Sun, 27 Mar 2016 17:15:19 GMT]]></title><description><![CDATA[<p>Das Problem mit XML oder JSON wäre ein erhöhter overhead und bei meinem Problem geht es darum teilweise sechzig mal die Sekunde große Datenmengen zu übertragen.</p>
<p>Ich mache das mit einem DataoutputStream:</p>
<pre><code>DataOutputStream outputStream;
...
Translation tr = data.get(i).read();

outputStream.writeInt(i);
outputStream.writeInt(data.get(i).getParentID());
outputStream.writeDouble(tr.getX());
outputStream.writeDouble(tr.getY());
outputStream.writeDouble(tr.getZ());
outputStream.writeDouble(tr.getA());
outputStream.writeDouble(tr.getB());
outputStream.writeDouble(tr.getC());

...
outputStream.flush();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2491616</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491616</guid><dc:creator><![CDATA[lonol15]]></dc:creator><pubDate>Sun, 27 Mar 2016 17:15:19 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Sun, 27 Mar 2016 17:46:27 GMT]]></title><description><![CDATA[<p>Der DataOutputStream dreht die Bytes um, er schreibt in Big Endian. Siehe <a href="https://en.wikipedia.org/wiki/Endianness" rel="nofollow">https://en.wikipedia.org/wiki/Endianness</a>. Wie floats und doubles geschrieben werden, kannst du vermutlich der Dokumentation entnehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491622</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491622</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Sun, 27 Mar 2016 17:46:27 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Sun, 27 Mar 2016 18:07:56 GMT]]></title><description><![CDATA[<p>d.h. deine &quot;int32 bitsToInt(uint8* bytes)&quot; ist der total falsche und unnötige Ansatz</p>
<p>du musst nur high und low bytes verdrehen, bei float und double koennte es genau so sein</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491624</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Sun, 27 Mar 2016 18:07:56 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Sun, 27 Mar 2016 18:16:02 GMT]]></title><description><![CDATA[<p>Für Integer sollte ntohl die Bytes richtig drehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491625</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491625</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sun, 27 Mar 2016 18:16:02 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Sun, 27 Mar 2016 19:44:36 GMT]]></title><description><![CDATA[<p>Danke für die Antworten, dann werde ich erstmal versuchen das auf der JAVA Seite mit einem OutpuStrem zu beheben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491630</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491630</guid><dc:creator><![CDATA[lonol15]]></dc:creator><pubDate>Sun, 27 Mar 2016 19:44:36 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Tue, 29 Mar 2016 13:39:23 GMT]]></title><description><![CDATA[<p>Also ich habe das jetzt auf der JAVA Seite erfolgreich gelöst:<br />
Den DataOutputStream kann man behalten, wenn man davor die bytes umdreht. Bei Integern geht das mit</p>
<pre><code>Integer.reverseBytes(int x)
</code></pre>
<p>. Bei double wird es schon ein bisschen umständlicher. Da habe ich mir eine kleine Hilfsfunktion geschrieben:</p>
<pre><code>public static double reverse(double in){
        return Double.longBitsToDouble(Long.reverseBytes(Double.doubleToLongBits(in)));
    }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2491767</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491767</guid><dc:creator><![CDATA[lonol15]]></dc:creator><pubDate>Tue, 29 Mar 2016 13:39:23 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Tue, 29 Mar 2016 13:42:55 GMT]]></title><description><![CDATA[<p>du könntest ja den float Wert aufteilen auf: Vorzeichen, Mantisse, Exponent.<br />
Die drei Werte speicherst du getrennt (z.B. als int oder als string) und lädst auch wieder getrennt rein - dann musst du nicht auf Bitebene rumbasteln.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491768</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491768</guid><dc:creator><![CDATA[mnmnbmn]]></dc:creator><pubDate>Tue, 29 Mar 2016 13:42:55 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Tue, 29 Mar 2016 15:41:09 GMT]]></title><description><![CDATA[<p>mnmnbmn schrieb:</p>
<blockquote>
<p>du könntest ja den float Wert aufteilen auf: Vorzeichen, Mantisse, Exponent ..... dann musst du nicht auf Bitebene rumbasteln.</p>
</blockquote>
<p>Ah ja ... und wie teilt man das auf, ohne auf Bitebene rumzubasteln?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491776</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491776</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Tue, 29 Mar 2016 15:41:09 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Tue, 29 Mar 2016 17:35:12 GMT]]></title><description><![CDATA[<p>lonol15 schrieb:</p>
<blockquote>
<pre><code>public static double reverse(double in){
        return Double.longBitsToDouble(Long.reverseBytes(Double.doubleToLongBits(in)));
    }
</code></pre>
</blockquote>
<p>Keine gute Idee.<br />
Was wenn die falsche Byte-Reihenfolge einen signaling <code>NaN</code> darstellt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491784</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491784</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 29 Mar 2016 17:35:12 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Wed, 30 Mar 2016 01:18:12 GMT]]></title><description><![CDATA[<p>Belli schrieb:</p>
<blockquote>
<p>mnmnbmn schrieb:</p>
<blockquote>
<p>du könntest ja den float Wert aufteilen auf: Vorzeichen, Mantisse, Exponent ..... dann musst du nicht auf Bitebene rumbasteln.</p>
</blockquote>
<p>Ah ja ... und wie teilt man das auf, ohne auf Bitebene rumzubasteln?</p>
</blockquote>
<p>z.B. mit <code>frexp</code> &amp; Co.<br />
<a href="http://www.cplusplus.com/reference/cmath/frexp/" rel="nofollow">http://www.cplusplus.com/reference/cmath/frexp/</a></p>
<p>Die Mantisse bekommt man dabei wieder als <code>double</code> .<br />
Macht aber nix, da man dann weiss dass die sich irgendwo im Bereich [0.5 ... 1) bewegt.<br />
Bzw. wenn negativ dann halt (-1 ... -0.5].</p>
<p>D.h. man kann erstmal das Vorzeichen notieren und rausrechnen und dann einfach so lange<br />
* verdoppeln<br />
* wenn &gt;= 1 dann 1 abziehen<br />
* wiederholen<br />
bis der Wert 0 ist.<br />
Bzw. einfach N mal wiederholen (mit N = grösste zu erwartende Mantissenbreite).<br />
Und bei jedem Durchlauf ne 1 bzw. 0 notieren, je nachdem ob man 1 abgezogen hat oder nicht.</p>
<p>Und den Exponenten bekommt man von <code>frexp</code> ja sowieso schon als Integer.</p>
<p>Alles ohne Bitfummelei.<br />
Die umgekehrte Richtung sollte trivial sein.</p>
<p>Dann fehlen bloss noch die Spezialwerte (NaNs, INFs, ...).<br />
Vorzeichen für 0 und die Spezielwerte kann man mit <code>copysign</code> abfragen (und erzeugen).<br />
<a href="http://en.cppreference.com/w/cpp/numeric/math/copysign" rel="nofollow">http://en.cppreference.com/w/cpp/numeric/math/copysign</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491810</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491810</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 30 Mar 2016 01:18:12 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von JAVA double in C++ double on Wed, 30 Mar 2016 04:27:13 GMT]]></title><description><![CDATA[<p>ist google so schwer?</p>
<p><a href="http://www.java2s.com/Code/Java/Language-Basics/Utilityforbyteswappingofalljavadatatypes.htm" rel="nofollow">http://www.java2s.com/Code/Java/Language-Basics/Utilityforbyteswappingofalljavadatatypes.htm</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2491816</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2491816</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Wed, 30 Mar 2016 04:27:13 GMT</pubDate></item></channel></rss>