<?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[streamreading (java to c++)]]></title><description><![CDATA[<p>Ich möchte folgende Java-Funktion in C++ nachprogrammieren:</p>
<p>(stream ist ein InputStream)</p>
<pre><code>protected long readLong() throws IOException {
		byte[] bytes = new byte[8];
		this.stream.read(bytes);
		return ((long) (bytes[7] &amp; 0xff) &lt;&lt; 56) | ((long) (bytes[6] &amp; 0xff) &lt;&lt; 48) | ((long) (bytes[5] &amp; 0xff) &lt;&lt; 40) | ((long) (bytes[4] &amp; 0xff) &lt;&lt; 32) |
			   ((long) (bytes[3] &amp; 0xff) &lt;&lt; 24) | ((long) (bytes[2] &amp; 0xff) &lt;&lt; 16) | ((long) (bytes[1] &amp; 0xff) &lt;&lt; 8) | (bytes[0] &amp; 0xff);
	}
</code></pre>
<p>meine vorläufige c++ variante sieht so aus, wobei stream ein ifstream ist:</p>
<pre><code>long Test::readLong()
{
	char* bytes = new char[8];
	stream.read(bytes, 8); 

	return ((long)(bytes[7] &amp; 0xFF) &lt;&lt; 56) | ((long)(bytes[6] &amp; 0xFF) &lt;&lt; 48) | ((long)(bytes[5] &amp; 0xFF) &lt;&lt; 40) | ((long)(bytes[4] &amp; 0xFF) &lt;&lt; 32) | 
		   ((long)(bytes[3] &amp; 0xFF) &lt;&lt; 24) | ((long)(bytes[2] &amp; 0xFF) &lt;&lt; 16) | ((long)(bytes[1] &amp; 0xFF) &lt;&lt; 8) | (bytes[0] &amp; 0xFF); 
}
</code></pre>
<p>Beim kompilieren bekomme ich nun aber diese Warnung:<br />
&quot;'&lt;&lt;' shift count negative or too big, undefined behaviour&quot;</p>
<p>Hat jemand eine Idee wie ich den Code richtig in C++ übersetzen kann?</p>
<p>Vielen Dank schon mal!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/267789/streamreading-java-to-c</link><generator>RSS for Node</generator><lastBuildDate>Wed, 02 Sep 2026 06:57:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/267789.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 30 May 2010 14:28:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to streamreading (java to c++) on Sun, 30 May 2010 14:28:09 GMT]]></title><description><![CDATA[<p>Ich möchte folgende Java-Funktion in C++ nachprogrammieren:</p>
<p>(stream ist ein InputStream)</p>
<pre><code>protected long readLong() throws IOException {
		byte[] bytes = new byte[8];
		this.stream.read(bytes);
		return ((long) (bytes[7] &amp; 0xff) &lt;&lt; 56) | ((long) (bytes[6] &amp; 0xff) &lt;&lt; 48) | ((long) (bytes[5] &amp; 0xff) &lt;&lt; 40) | ((long) (bytes[4] &amp; 0xff) &lt;&lt; 32) |
			   ((long) (bytes[3] &amp; 0xff) &lt;&lt; 24) | ((long) (bytes[2] &amp; 0xff) &lt;&lt; 16) | ((long) (bytes[1] &amp; 0xff) &lt;&lt; 8) | (bytes[0] &amp; 0xff);
	}
</code></pre>
<p>meine vorläufige c++ variante sieht so aus, wobei stream ein ifstream ist:</p>
<pre><code>long Test::readLong()
{
	char* bytes = new char[8];
	stream.read(bytes, 8); 

	return ((long)(bytes[7] &amp; 0xFF) &lt;&lt; 56) | ((long)(bytes[6] &amp; 0xFF) &lt;&lt; 48) | ((long)(bytes[5] &amp; 0xFF) &lt;&lt; 40) | ((long)(bytes[4] &amp; 0xFF) &lt;&lt; 32) | 
		   ((long)(bytes[3] &amp; 0xFF) &lt;&lt; 24) | ((long)(bytes[2] &amp; 0xFF) &lt;&lt; 16) | ((long)(bytes[1] &amp; 0xFF) &lt;&lt; 8) | (bytes[0] &amp; 0xFF); 
}
</code></pre>
<p>Beim kompilieren bekomme ich nun aber diese Warnung:<br />
&quot;'&lt;&lt;' shift count negative or too big, undefined behaviour&quot;</p>
<p>Hat jemand eine Idee wie ich den Code richtig in C++ übersetzen kann?</p>
<p>Vielen Dank schon mal!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1904518</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1904518</guid><dc:creator><![CDATA[hanzpetr]]></dc:creator><pubDate>Sun, 30 May 2010 14:28:09 GMT</pubDate></item><item><title><![CDATA[Reply to streamreading (java to c++) on Sun, 30 May 2010 14:32:20 GMT]]></title><description><![CDATA[<p>long ist bei dir wahrscheinlich nicht 64 bit breit. Nimm stattdessen int64_t.<br />
char würde ich auch nicht nehmen, wenn du mit Bytes hantierst. Mach dir lieber einen typedef unsigned char byte;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1904519</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1904519</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Sun, 30 May 2010 14:32:20 GMT</pubDate></item><item><title><![CDATA[Reply to streamreading (java to c++) on Sun, 30 May 2010 14:36:23 GMT]]></title><description><![CDATA[<p>und statt</p>
<pre><code class="language-cpp">char* bytes = new char[8];
</code></pre>
<pre><code class="language-cpp">char bytes[8];
</code></pre>
<p>damit das ganze auch wieder gelöscht wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1904521</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1904521</guid><dc:creator><![CDATA[brotbernd]]></dc:creator><pubDate>Sun, 30 May 2010 14:36:23 GMT</pubDate></item><item><title><![CDATA[Reply to streamreading (java to c++) on Sun, 30 May 2010 14:39:38 GMT]]></title><description><![CDATA[<p>danke! int64_t müsste ich vermutlich selbst definieren, aber mit long long funktioniertes auch.</p>
<p>Bezüglich char: ich hatte eigentlich vor unsigned char zu verwenden, aber die read methode verlangt einen char als parameter. Könnte es dann mit unsigned nicht zu problemen kommen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1904523</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1904523</guid><dc:creator><![CDATA[hanzpetr]]></dc:creator><pubDate>Sun, 30 May 2010 14:39:38 GMT</pubDate></item><item><title><![CDATA[Reply to streamreading (java to c++) on Sun, 30 May 2010 14:44:51 GMT]]></title><description><![CDATA[<p>brotbernd schrieb:</p>
<blockquote>
<p>und statt</p>
<pre><code class="language-cpp">char* bytes = new char[8];
</code></pre>
<pre><code class="language-cpp">char bytes[8];
</code></pre>
<p>damit das ganze auch wieder gelöscht wird.</p>
</blockquote>
<p>diese variante wollte ich auch ursprünglich verwenden, hatte damit aber auch probleme mit der read methode, da ich dann diesen kompilierfehler bekam:</p>
<p>Error 1 error C2664: 'std::basic_istream&lt;_Elem,_Traits&gt;::read' : cannot convert parameter 1 from 'char (*)[4]' to 'char *'</p>
<p>also wenn ich bei meiner variante bleibe, müsste ich also noch ein delete bytes einfugen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1904525</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1904525</guid><dc:creator><![CDATA[hanzpetr]]></dc:creator><pubDate>Sun, 30 May 2010 14:44:51 GMT</pubDate></item><item><title><![CDATA[Reply to streamreading (java to c++) on Sun, 30 May 2010 14:52:45 GMT]]></title><description><![CDATA[<p>int64_t &amp; Co. bekommst du mit &lt;stdint.h&gt;.<br />
Warum eine read-Funktion char* als Parameter erwartet ist mir zwar rätselhaft, aber du kannst ihr gefahrlos einen unsigned char-Zeiger geben. Der benötigte Cast ist natürlich nicht so schön.</p>
<p>Edit:</p>
<blockquote>
<pre><code>Error	1	error C2664: 'std::basic_istream&lt;_Elem,_Traits&gt;::read' : cannot convert parameter 1 from 'char (*)[4]' to 'char *'
</code></pre>
</blockquote>
<p>Du hast char* bytes[8]; geschrieben.</p>
<p>Edit2:<br />
Und auch dann müsste es delete[] bytes heißen.</p>
<p>Wozu dienen eigentlich die ganzen &amp;0xff? Das scheint mir weder in Java noch in C++ viel Sinn zu machen, denn größer als 0xff können Bytes sowieso nicht sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1904527</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1904527</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Sun, 30 May 2010 14:52:45 GMT</pubDate></item><item><title><![CDATA[Reply to streamreading (java to c++) on Sun, 30 May 2010 15:33:48 GMT]]></title><description><![CDATA[<p>stdint.h kennt der compiler nicht (Visual Studio 2008).. also bleibe ich vorläufig einfach bei long long.</p>
<p>den cast habe ich nur mit reinterpret_cast hinbekommen, ich denke das wird in diesem fall schon ok sein.</p>
<p>die '&amp; 0xFF' verstehe ich auch nicht. ich habe nur den java code vorliegen, den ich nun nach c++ überführen soll und so weit ich weiß funktioniert der java-code, also habe ich das einfach übernommen.</p>
<p>Edit:<br />
vielleicht werden die 0xFF's in Java deswegen benötigt, da es in java keine unsigned bytes gibt und auf diese Weise das Vorzeichen beseitigt wird?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1904545</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1904545</guid><dc:creator><![CDATA[hanzpetr]]></dc:creator><pubDate>Sun, 30 May 2010 15:33:48 GMT</pubDate></item><item><title><![CDATA[Reply to streamreading (java to c++) on Sun, 30 May 2010 16:08:56 GMT]]></title><description><![CDATA[<p>Im Prinzip ist es vollkommen wurst, ob <code>char</code> oder <code>unsigned char</code> , die ham ja eh dieselbe Bitrepräsentation und da ja hier nur mit Bits gearbeitet wird, kann man beides nehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1904575</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1904575</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Sun, 30 May 2010 16:08:56 GMT</pubDate></item><item><title><![CDATA[Reply to streamreading (java to c++) on Thu, 03 Jun 2010 13:12:27 GMT]]></title><description><![CDATA[<p>ich hätte hier noch ein weiteres Problem: und zwar muss ich UTF-8 codierte String einlesen.</p>
<pre><code class="language-java">protected String readString(int size, int len, String charset) throws IOException
{
	int length = (size &gt; 0 ? size : len);
	byte[] bytes = new byte[length];
	this.stream.read(bytes);
	length = (len &gt;= 0 ? len : size); 

	try
	{
		return new String(new String(bytes, 0, length, charset).getBytes(&quot;UTF-8&quot;), &quot;UTF-8&quot;);
	}
	catch(Throwable e)
	{
		e.printStackTrace();
	}
	return new String(bytes, 0, length);
}
</code></pre>
<p>hat jemand eine Idee wie ich den Code ab dem try in C++ nachprogrammiere?</p>
<p>Zur Zeit sieht der Code so aus, ich komme leider nur bis zum einlesen der Daten. Wie kann ich den char* in einen UTF-8 string casten bzw. welcher Datentyp ist dafür in c++ vorgesehen? So weit ich weiß ist ein wchar_t ja ein 16-bit Unicode, ein char* zwar 8-bit aber wieder ASCII codiert.</p>
<pre><code class="language-cpp">??? Test::readString(int size, int len)
{
	int length = (size &gt; 0 ? size : len);
	unsigned char* bytes = new unsigned char[length];

	stream.read(reinterpret_cast&lt;char*&gt;(bytes), length); 

}
</code></pre>
<p>Ich hoffe jemand kennt sich damit aus, und auch vielen Dank für die bisherigen Beiträge!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1904649</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1904649</guid><dc:creator><![CDATA[hanzpetr]]></dc:creator><pubDate>Thu, 03 Jun 2010 13:12:27 GMT</pubDate></item></channel></rss>